⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 irlog2rlog.c

📁 fortran并行计算包
💻 C
📖 第 1 页 / 共 2 页
字号:
    int rank = pLevel->rank;    while (pLevel)    {	if (pLevel->rank > rank)	    rank = pLevel->rank;	pLevel = pLevel->next;    }    return rank;}RecursionStruct *FindMinLevel(RecursionStruct *pLevel){    RecursionStruct *pRet;    pRet = pLevel;    while (pLevel)    {	if (pLevel->rank < pRet->rank)	{	    pRet = pLevel;	}	else	{	    if (pLevel->rank == pRet->rank && pLevel->level < pRet->level)	    {		pRet = pLevel;	    }	}	pLevel = pLevel->next;    }    return pRet;}int FindMaxRecursion(RecursionStruct *pLevel, int rank){    int nMax = 0;    while (pLevel)    {	if (pLevel->rank == rank)	{	    nMax = (pLevel->level > nMax) ? pLevel->level : nMax;	}	pLevel = pLevel->next;    }    return nMax+1; /* return nMax + 1 because the recursion levels begin with zero */}void RemoveLevel(int rank){    RecursionStruct *pLevel = g_pLevel, *pTrailer = g_pLevel;    while (pLevel)    {	if (pLevel->rank == rank)	{	    if (pLevel == pTrailer)	    {		if (g_pLevel == pLevel)		    g_pLevel = g_pLevel->next;		pLevel = pLevel->next;		fclose(pTrailer->fout);		unlink(pTrailer->filename);		MPIU_Free(pTrailer);		pTrailer = pLevel;	    }	    else	    {		pTrailer->next = pLevel->next;		fclose(pLevel->fout);		unlink(pLevel->filename);		MPIU_Free(pLevel);		pLevel = pTrailer->next;	    }	}	else	{	    if (pTrailer != pLevel)		pTrailer = pTrailer->next;	    pLevel = pLevel->next;	}    }}BOOL GetNextArrow(IRLOG_IOStruct *pInput){    do    {	if (IRLOG_GetNextRecord(pInput))	{	    IRLOG_CloseInputStruct(&pInput);	    return FALSE;	}    } while (pInput->header.type != RLOG_IARROW_TYPE);    return TRUE;}void ReadAllArrows(IRLOG_IOStruct **ppInput, int nNumInputs){    int i, j;    double dMin;    int index;    if (nNumInputs < 1)	return;    /* make sure all the inputs are currently on an iarrow record */    for (i=0; i<nNumInputs; i++)    {	if (ppInput[i]->header.type != RLOG_IARROW_TYPE)	{	    if (!GetNextArrow(ppInput[i]))	    {		for (j=i; j<nNumInputs-1; j++)		    ppInput[j] = ppInput[j+1];		nNumInputs--;		i--;	    }	}    }    /* read iarrow records and save them to the arrow file */    /* until all the inputs are exhausted */    while (nNumInputs > 0)    {	/* find the earliest record */	dMin = ppInput[0]->record.iarrow.timestamp;	index = 0;	for (i=1; i<nNumInputs; i++)	{	    if (ppInput[i]->record.iarrow.timestamp < dMin)	    {		dMin = ppInput[i]->record.iarrow.timestamp;		index = i;	    }	}	/* save the record */	SaveArrow(&ppInput[index]->record.iarrow);	/* read the next iarrow record from the same source */	if (!GetNextArrow(ppInput[index]))	{	    for (i=index; i<nNumInputs-1; i++)		ppInput[i] = ppInput[i+1];	    nNumInputs--;	}    }}BOOL IsNumber(char *str){    if (str == NULL)	return FALSE;    while (*str != '\0')    {	if (!isdigit(*str))	    return FALSE;	str++;    }    return TRUE;}static BOOL s_bFreeArgv = FALSE;void GenerateNewArgv(int *pargc, char ***pargv, int n){    int argc;    char **argv;    int length, i;    char *buffer, *str;    length = (sizeof(char*) * (n+3)) +strlen((*pargv)[0]) + 1 + strlen((*pargv)[1]) + 1 + (15 * n);    buffer = (char*)MPIU_Malloc(length);    argc = n+2;    argv = (char**)buffer;    str = buffer + (sizeof(char*) * (n+4));    argv[0] = str;    str += sprintf(str, "%s", (*pargv)[0]);    *str++ = '\0';    argv[1] = str;    str += sprintf(str, "%s", (*pargv)[1]);    *str++ = '\0';    for (i=0; i<n; i++)    {	argv[i+2] = str;	str += sprintf(str, "log%d.irlog", i);	*str++ = '\0';    }    argv[n+3] = NULL;    *pargc = argc;    *pargv = argv;    s_bFreeArgv = TRUE;}int main(int argc, char *argv[]){    RLOG_FILE_HEADER header;    RLOG_State_list *pState;    int nNumInputs;    IRLOG_IOStruct *pInput;    IRLOG_IOStruct **ppInput;    int nMaxRank = 0;    int nMinRank = MAX_RANK;    int nNumStates = 0;    int type, length;    int nMaxLevel = 0;    int nNumLevels = 0;    int nTotalNumEvents = 0;    int nNumEvents;    RecursionStruct *pLevel;    FILE *fout;    int i, j;    int nRank;    if (argc < 3)    {	MPIU_Error_printf("Usage:\nirlog2rlog out.rlog in0.irlog in1.irlog ...\nirlog2rlog out.rlog n\n");	return 0;    }    if (argc == 3 && IsNumber(argv[2]))    {	GenerateNewArgv(&argc, &argv, atoi(argv[2]));    }    nNumInputs = argc - 2;    /* open the output rlog file */    fout = fopen(argv[1], "wb");    if (fout == NULL)    {	MPIU_Error_printf("unable to open output file '%s'\n", argv[1]);	return -1;    }    /* read the arrows from all the files in order */    ppInput = (IRLOG_IOStruct**)MPIU_Malloc(nNumInputs * sizeof(IRLOG_IOStruct*));    for (i=0; i<nNumInputs; i++)    {	ppInput[i] = IRLOG_CreateInputStruct(argv[i+2]);	if (ppInput[i] == NULL)	{	    MPIU_Error_printf("Unable to create an input structure for '%s', skipping\n", argv[i+2]);	}    }    for (i=0; i<nNumInputs; i++)    {	if (ppInput[i] == NULL)	{	    for (j=i; j<nNumInputs-1; j++)		ppInput[j] = ppInput[j+1];	    nNumInputs--;	    i--;	}    }    MPIU_Msg_printf("reading the arrows from all the input files.\n");fflush(stdout);    ReadAllArrows(ppInput, nNumInputs);    nNumInputs = argc - 2;    /* read, parse and save all the data from the irlog files */    for (i=0; i<nNumInputs; i++)    {	pInput = IRLOG_CreateInputStruct(argv[i+2]);	if (pInput == NULL)	{	    MPIU_Error_printf("Unable to create an input structure for '%s', skipping\n", argv[i+2]);	}	else	{	    MPIU_Msg_printf("reading irlog file: %s\n", argv[i+2]);fflush(stdout);	    for(;;)	    {		switch (pInput->header.type)		{		case RLOG_STATE_TYPE:		    SaveState(&pInput->record.state);		    break;		case RLOG_COMM_TYPE:		    nMaxRank = (pInput->record.comm.rank > nMaxRank) ? pInput->record.comm.rank : nMaxRank;		    nMinRank = (pInput->record.comm.rank < nMinRank) ? pInput->record.comm.rank : nMinRank;		    break;		case RLOG_IARROW_TYPE:		    /* SaveArrow(&pInput->record.iarrow); */		    break;		case RLOG_EVENT_TYPE:		    SaveEvent(&pInput->record.event);		    break;		default:		    MPIU_Error_printf("Unknown irlog record type: %d\n", pInput->header.type);		    break;		}				if (IRLOG_GetNextRecord(pInput))		{		    IRLOG_CloseInputStruct(&pInput);		    break;		}	    }	}    }    /* set the fields in the header */    header.nMinRank = FindMinRank(g_pLevel);    header.nMaxRank = FindMaxRank(g_pLevel);    if (nMinRank != header.nMinRank)	MPIU_Error_printf("minimum event rank %d does not equal the minimum comm record rank %d\n", header.nMinRank, nMinRank);    if (nMaxRank != header.nMaxRank)	MPIU_Error_printf("maximum event rank %d does not equal the maximum comm record rank %d\n", header.nMaxRank, nMaxRank);    /* write header */    MPIU_Msg_printf("writing header.\n");fflush(stdout);    type = RLOG_HEADER_SECTION;    length = sizeof(RLOG_FILE_HEADER);    /* fwrite(&type, sizeof(int), 1, fout); */    WriteFileData(&type, sizeof(int), fout);    /* fwrite(&length, sizeof(int), 1, fout);*/    WriteFileData(&length, sizeof(int), fout);    /* fwrite(&header, sizeof(RLOG_FILE_HEADER), 1, fout); */    WriteFileData(&header, sizeof(RLOG_FILE_HEADER), fout);    /* write states */    if (g_pList)    {	MPIU_Msg_printf("writing states.\n");fflush(stdout);    }    pState = g_pList;    while (pState)    {	nNumStates++;	pState = pState->next;    }    type = RLOG_STATE_SECTION;    length = nNumStates * sizeof(RLOG_STATE);    /* fwrite(&type, sizeof(int), 1, fout); */    WriteFileData(&type, sizeof(int), fout);    /* fwrite(&length, sizeof(int), 1, fout); */    WriteFileData(&length, sizeof(int), fout);    pState = g_pList;    while (pState)    {	/* fwrite(pState, sizeof(RLOG_STATE), 1, fout); */	WriteFileData(pState, sizeof(RLOG_STATE), fout);	pState = pState->next;    }    /* write arrows */    if (g_fArrow)    {	MPIU_Msg_printf("writing arrows.\n");fflush(stdout);	type = RLOG_ARROW_SECTION;	length = ftell(g_fArrow);	/* fwrite(&type, sizeof(int), 1, fout); */	WriteFileData(&type, sizeof(int), fout);	/* fwrite(&length, sizeof(int), 1, fout); */	WriteFileData(&length, sizeof(int), fout);	AppendFile(fout, g_fArrow);    }    /* write events */    while (g_pLevel)    {	pLevel = FindMinLevel(g_pLevel);	nNumLevels = FindMaxRecursion(g_pLevel, pLevel->rank);	nRank = pLevel->rank;	/* count the events for this rank */	nNumEvents = 0;	for (i=0; i<nNumLevels; i++)	{	    pLevel = GetLevel(pLevel->rank, i);	    nNumEvents += pLevel->num_events;	}	/* write an event section for this rank */	type = RLOG_EVENT_SECTION;	length = sizeof(int) + sizeof(int) + (nNumLevels * sizeof(int)) + (nNumEvents * sizeof(RLOG_EVENT));	/* fwrite(&type, sizeof(int), 1, fout); */	WriteFileData(&type, sizeof(int), fout);	/* fwrite(&length, sizeof(int), 1, fout); */	WriteFileData(&length, sizeof(int), fout);        /* fwrite(&nRank, sizeof(int), 1, fout); */	WriteFileData(&nRank, sizeof(int), fout);	/* fwrite(&nNumLevels, sizeof(int), 1, fout); */	WriteFileData(&nNumLevels, sizeof(int), fout);	for (i=0; i<nNumLevels; i++)	{	    pLevel = GetLevel(nRank, i);	    /* fwrite(&pLevel->num_events, sizeof(int), 1, fout); */	    WriteFileData(&pLevel->num_events, sizeof(int), fout);	}	for (i=0; i<nNumLevels; i++)	{	    MPIU_Msg_printf("writing event level %d:%d\n", nRank, i);fflush(stdout);	    pLevel = GetLevel(nRank, i);	    AppendFile(fout, pLevel->fout);	}	/* remove this rank from the list of levels */	RemoveLevel(nRank);    }    /* free resources */    while (g_pList)    {	pState = g_pList;	g_pList = g_pList->next;	MPIU_Free(pState);    }    if (g_fArrow)    {	fclose(g_fArrow);	unlink(g_pszArrowFilename);    }    if (s_bFreeArgv)	MPIU_Free(argv);    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -