7 // Check if array == refArray
8 void compareArray(const char* ID
, const void* array
, const void* refArray
, int size
,
11 Real EPS
= 1e-5; //precision
12 printf("Checking %s\n",ID
);
14 for (int i
=0; i
<size
; i
++)
16 Real error
= isinteger
17 ? fabs(((int*)array
)[i
] - ((int*)refArray
)[i
])
18 : fabs(((Real
*)array
)[i
] - ((Real
*)refArray
)[i
]);
19 if (error
>= maxError
)
23 printf(" Inaccuracy: max(abs(error)) = %g >= %g\n",maxError
,EPS
);
28 void compareArray_real(const char* ID
, const void* array
, const void* refArray
, int size
)
30 return compareArray(ID
, array
, refArray
, size
, 0);
33 void compareArray_int(const char* ID
, const void* array
, const void* refArray
, int size
)
35 return compareArray(ID
, array
, refArray
, size
, 1);
38 // Read array by columns (as in MATLAB) and return by-rows encoding
39 void* readArray(const char* fileName
, int isinteger
)
41 // need to prepend 'data/' (not really nice code...)
42 char* fullFileName
= (char*)calloc(5+strlen(fileName
)+1, sizeof(char));
43 strcat(fullFileName
, "data/");
44 strcat(fullFileName
, fileName
);
46 // first pass to know how many elements to allocate
47 char* command
= (char*)calloc(12+strlen(fullFileName
)+8+1, sizeof(char));
48 strcat(command
, "wc -l ");
49 strcat(command
, fullFileName
);
50 FILE *arraySize
= popen(command
, "r");
52 char* bufferNum
= (char*)calloc(64, sizeof(char));
53 fgets(bufferNum
, sizeof(bufferNum
), arraySize
);
54 int n
= atoi(bufferNum
);
57 // open file for reading
58 FILE* arrayFile
= fopen(fullFileName
, "r");
61 // read all values, and convert them to by-rows matrices format
62 size_t elementSize
= isinteger
? sizeof(int) : sizeof(Real
);
63 void* array
= malloc(n
*elementSize
);
64 for (int i
=0; i
<n
; i
++)
66 fgets(bufferNum
, 64, arrayFile
);
67 // transform buffer content into Real or int, and store it at appropriate location
69 ((int*)array
)[i
] = atoi(bufferNum
);
71 ((Real
*)array
)[i
] = atof(bufferNum
);
79 int* readArray_int(const char* fileName
)
81 return (int*)readArray(fileName
, 1);
84 Real
* readArray_real(const char* fileName
)
86 return (Real
*)readArray(fileName
, 0);
89 int read_int(const char* fileName
)
91 int* array
= readArray_int(fileName
);
97 Real
read_real(const char* fileName
)
99 Real
* array
= readArray_real(fileName
);