| 1 | // Check if array == refArray |
| 2 | void compareArray(const char* ID, const void* array, const void* refArray, int size, |
| 3 | int isinteger) |
| 4 | { |
| 5 | float EPS = 1e-5; //precision |
| 6 | printf("Checking %s\n",ID); |
| 7 | float maxError = 0.0; |
| 8 | for (int i=0; i<size; i++) |
| 9 | { |
| 10 | float error = isinteger |
| 11 | ? fabs(((int*)array)[i] - ((int*)refArray)[i]) |
| 12 | : fabs(((float*)array)[i] - ((float*)refArray)[i]); |
| 13 | if (error >= maxError) |
| 14 | maxError = error; |
| 15 | } |
| 16 | if (maxError >= EPS) |
| 17 | printf(" Inaccuracy: max(abs(error)) = %g >= %g\n",maxError,EPS); |
| 18 | else |
| 19 | printf(" OK\n"); |
| 20 | } |
| 21 | |
| 22 | void compareArray_real(const char* ID, const void* array, const void* refArray, int size) |
| 23 | { |
| 24 | return compareArray(ID, array, refArray, size, 0); |
| 25 | } |
| 26 | |
| 27 | void compareArray_int(const char* ID, const void* array, const void* refArray, int size) |
| 28 | { |
| 29 | return compareArray(ID, array, refArray, size, 1); |
| 30 | } |
| 31 | |
| 32 | // Read array by columns (as in MATLAB) and return by-rows encoding |
| 33 | void* readArray(const char* fileName, int isinteger) |
| 34 | { |
| 35 | // need to prepend '../data/' (not really nice code...) |
| 36 | char* fullFileName = (char*)calloc(5+strlen(fileName)+1, sizeof(char)); |
| 37 | strcat(fullFileName, "data/"); |
| 38 | strcat(fullFileName, fileName); |
| 39 | |
| 40 | // first pass to know how many elements to allocate |
| 41 | char* command = (char*)calloc(12+strlen(fullFileName)+8+1, sizeof(char)); |
| 42 | strcat(command, "grep -o ' ' "); |
| 43 | strcat(command, fullFileName); |
| 44 | strcat(command, " | wc -l"); |
| 45 | FILE *countSpaces = popen(command, "r"); |
| 46 | char* buffer = (char*)calloc(32, sizeof(char)); |
| 47 | fgets(buffer, sizeof(buffer), command); |
| 48 | int n = atoi(buffer) + 1; |
| 49 | free(buffer); |
| 50 | pclose(countSpaces); |
| 51 | |
| 52 | // open file for reading |
| 53 | FILE* file = fopen(fullFileName, "r"); |
| 54 | free(fullFileName); |
| 55 | |
| 56 | int d = 1; |
| 57 | size_t elementSize = isinteger |
| 58 | ? sizeof(int) |
| 59 | : sizeof(float); |
| 60 | |
| 61 | // read all values, and convert them to by-rows matrices format |
| 62 | void* array = malloc(n*elementSize); |
| 63 | char curChar = ' '; |
| 64 | char bufferNum[64]; |
| 65 | for (int u=0; u<n; u++) |
| 66 | { |
| 67 | // read number (as a string) |
| 68 | int bufferIndex = 0; |
| 69 | while (!feof(file) && curChar!=' ') |
| 70 | { |
| 71 | curChar = fgetc(file); |
| 72 | bufferNum[bufferIndex++] = curChar; |
| 73 | } |
| 74 | bufferNum[bufferIndex] = 0; |
| 75 | // transform string into float, and store it at appropriate location |
| 76 | if (isinteger) |
| 77 | ((int*)array)[u] = atoi(bufferNum); |
| 78 | else |
| 79 | ((float*)array)[u] = atof(bufferNum); |
| 80 | // position to next non-separator character |
| 81 | curChar = fgetc(file); |
| 82 | } |
| 83 | fclose(file); |
| 84 | |
| 85 | return array; |
| 86 | } |
| 87 | |
| 88 | int* readArray_int(const char* fileName) |
| 89 | { |
| 90 | return (int*)readArray(fileName, 1); |
| 91 | } |
| 92 | |
| 93 | float* readArray_real(const char* fileName) |
| 94 | { |
| 95 | return (int*)readArray(fileName, 0); |
| 96 | } |
| 97 | |
| 98 | int read_int(const char* fileName) |
| 99 | { |
| 100 | return readArray_int(fileName)[0]; |
| 101 | } |
| 102 | |
| 103 | float read_real(const char* fileName) |
| 104 | { |
| 105 | return readArray_real(fileName)[0]; |
| 106 | } |