Code:
#include <stdio.h>
#include <time.h>
#ifdef PROFILE
#define MAX_TIMES 1000
static struct {
clock_t start;
clock_t created;
clock_t processed;
clock_t merged;
} times[1000];
static int nSample = 0;
#endif
void FormatMatrixComp(double *matrix, int *nmat, int *nrow, int *ncol, int *comp_flag,
int *idbaseMat, int *idtargetMat)
{
int i, j, k, p;
struct MG1G1_ *ptTargetMat; // will be used to point to target matrix depending on value of idtargetMat
// initialization of structures
// char * cpReadString = ( char * ) malloc ( 1024 ) ;
// char * cpFileName = ( char * ) malloc ( 1024 ) ;
char *cpFileName = NULL; // not needed in this version
FILE *fStream = NULL; // dummy file pointer
int nRow = 0;
int nColumn = 0;
int nComp = 0;
short tempMG1G1Created = 0;
static nrow_old = 0;
static firsttime = 1;
#ifdef PROFILE
times[nSample].start = clock();
#endif
// memory allocation of structure (only if dimension changed since last call)
// all other temporary structures used within this function call
// initialize ams structure to zero
for (nComp = 0; nComp < tempAMSdata.nComp_d; ++nComp) {
for (i = 0; i < tempAMSdata.stpDcomp[nComp].nStrip; ++i) {
for (j = 0; j < 10; ++j)
tempAMSdata.stpDcomp[nComp].fpProps[i][j] = 0;
}
for (i = 0; i < tempAMSdata.stpDcomp[nComp].nStrip; ++i) {
for (j = 0; j < 3; ++j)
tempAMSdata.stpDcomp[nComp].fpCoords[i][j] = 0;
}
}
// loop over all matrices
for (p = 0; p < *nmat; p++) {
lpdata.nBalasts = *nrow;
if (*nrow != nrow_old) {
// free memory allocated for structure in second and third dimension before reallocating first dimension
for (i = 0; i < nrow_old; ++i) {
for (j = 0; j < 1; ++j)
free(lpdata.stBalast[i].lpBalastMatrix[j]);
free(lpdata.stBalast[i].lpBalastMatrix);
}
// reallocate structure with new dimensions
lpdata.stBalast =
(struct balast_section *) realloc(lpdata.stBalast, sizeof(struct balast_section) * *nrow);
for (i = 0; i < *nrow; ++i) {
lpdata.stBalast[i].lpBalastMatrix = (double **) malloc(sizeof(double *) * 1);
for (j = 0; j < 1; ++j) {
lpdata.stBalast[i].lpBalastMatrix[j] = (double *) malloc(sizeof(double) * 10);
for (k = 0; k < 10; ++k) {
lpdata.stBalast[i].lpBalastMatrix[j][k] = 0; // initialize values to zero
}
}
}
// remember old dimensions
nrow_old = *nrow;
}
// storage of FORTRAN matrix
for (k = 0; k < *ncol; k++) {
for (i = 0; i < *nrow; i++) {
lpdata.stBalast[i].nRows = 1;
lpdata.stBalast[i].nColumns = *ncol;
lpdata.stBalast[i].lpBalastMatrix[0][k] = *(matrix++);
}
}
if (!createLPams(cpFileName, &stGeoxData, &stG2SData, &lpdata, &tempAMSdata, comp_flag)) {
// printf( "\n\ file creation Failed..............\n" ) ;
// fputs( "\file creation Failed..............", fStream ) ;
} else {
// printf( "\ file creation successful..............\n" ) ;
// fputs( "\ file creation successful..............", fStream ) ;
LPAMSCreated = 1;
}
if (LPAMSCreated == 1) {
// printf( "\ File creation is complete..............\n" ) ;
// fputs( "\ File creation is complete..........", fStream ) ;
}
(comp_flag++);
} // loop over all matrices
#ifdef PROFILE
times[nSample].created = clock();
#endif
// Write file for validation purposes
/*********************************************************************/
/* CREATE MATRIX FROM STRUCTURE ... */
/*********************************************************************/
/********************************Processing Files Operation *****************************/
stReturn = GetOnesMetrixDouble(tempMG1G1.dbpMG1G1_data, tempMG1G1.nRow, tempMG1G1.nColumn, 0); // initialize tempMG1G1 to zero
if (0 == ProcessMatrixData(&stProjectSpace, &tempAMSdata,
&stGeoxData, &stMatrixdata, &stG2SData, &tempMG1G1, fStream)) {
} else {
tempMG1G1Created = 1;
stReturn =
GetOnesMetrixDouble(stouttempMG1G1->dbpMG1G1_data, stouttempMG1G1->nRow,
stouttempMG1G1->nColumn, 0);
if (0 == ConvertToG1SixMatrix(&tempMG1G1, stouttempMG1G1)) {
stReturn = 0;
} else {
stReturn = 1;
}
}
#ifdef PROFILE
times[nSample].processed = clock();
#endif
/***************************************************************************************/
/* MERGE CREATED MATRIX WITH BASE MATRIX TO CREATE TARGET MATRIX ... */
/***************************************************************************************/
// select target matrix according to value of switch idbaseMat
switch (*idtargetMat) {
case OWE_ID:
ptTargetMat = &stoutMg1g1OWE;
break;
case ZFW_ID:
ptTargetMat = &stMg1g1ZFW;
break;
case CS_ID:
ptTargetMat = &stMg1g1CS;
break;
}
stReturn = GetOnesMetrixDouble(ptBaseMat->dbpMG1G1_data, ptBaseMat->nRow, ptBaseMat->nColumn, 0);
// initialize structure to zero
// copy values of original baseMat to temporary baseMat container row by row to prevent mixing up of data in the merging process
// (which matrix to select as baseMat depends on value of input flag idbaseMat)
for (nRow = 0; nRow < ptBaseMat->nRow; ++nRow) {
switch (*idbaseMat) {
case OWE_ID:
memcpy(ptBaseMat->dbpMG1G1_data[nRow], stoutMg1g1OWE.dbpMG1G1_data[nRow],
sizeof(double) * ptBaseMat->nColumn);
break;
case ZFW_ID:
memcpy(ptBaseMat->dbpMG1G1_data[nRow], stMg1g1ZFW.dbpMG1G1_data[nRow],
sizeof(double) * ptBaseMat->nColumn);
break;
case CS_ID:
memcpy(ptBaseMat->dbpMG1G1_data[nRow], stMg1g1CS.dbpMG1G1_data[nRow],
sizeof(double) * ptBaseMat->nColumn);
break;
}
}
// initialize values of target matrix and cg matrix to zero
stReturn =
GetOnesMetrixDouble(ptTargetMat->dbpMG1G1_data, ptTargetMat->nRow, ptTargetMat->nColumn, 0);
stReturn = GetOnesMetrixDouble(CGMatrix, ptTargetMat->nRow, 3, 0);
// merge created matrix with base matrix to create target matrix
if (0 == MergeMatrices(ptBaseMat, stouttempMG1G1, &stGeoxData, ptTargetMat, CGMatrix)) {
stReturn = 0;
} else {
stReturn = 1;
}
#ifdef PROFILE
times[nSample].merged = clock();
#endif
#ifdef PROFILE
{
// The array is full, dump the contents to a file
if ( ++nSample == MAX_TIMES ) {
FILE *fp = fopen("profiledata.txt","a");
if ( fp ) {
int i;
for ( i = 0 ; i < nSample ; i++ ) {
fprintf(fp,"%ld,%ld,%ld,%ld\n",
times[i].start, times[i].created,
times[i].processed, times[i].merged );
}
nSample = 0; // allow more data
fclose(fp);
} else {
// use perror() to find out why
fprintf(stderr,"Unable to open profiledata.txt\n");
}
}
}
#endif
} // end of function
To compile the code normally, you do this.