FK20 CUDA
parseFFTTest.c
Go to the documentation of this file.
1 //Reads a input file and parses its data
2 #define _GNU_SOURCE
3 #include<stdio.h>
4 #include<stdlib.h>
5 #include<stdint.h>
6 #include<string.h>
7 #include "parseFFTTest.h"
8 
9 void freeffttest_t( ffttest_t* fftTest){
10  // Because a part of the struct is dinamically allocated, you need to free a member of it
11  // Ignore this function under penalty of memory leak
12  free(fftTest->testCase);
13 
14 }
15 
16 void parsePoly(char *line, ffttest_t *fftTest, ssize_t line_read_size){
17  //fprintf(stderr, "<%s>\n", __func__);
18  char tmp[17];
19  if(strncmp(line, "polynomial ", 11) != 0){
20  printf("Fatal, malformed input: Expected polynomial keyword\n");
21  exit(1);
22  }
23 
24  ssize_t expectedLineSize = 11+(64*fftTest->polynomialLength)+fftTest->polynomialLength;
25  if(line_read_size != expectedLineSize){
26  printf("Fatal, malformed input: Expected %ld input size, read %ld\n", expectedLineSize, line_read_size);
27  exit(1);
28  }
29 
30  line += 11*sizeof(char);
31 
32  int testIdx=fftTest->nTest;
33  fftTest->testCase = realloc(fftTest->testCase, (testIdx+1)*sizeof(struct FFTTestCase));
34  memset(&(fftTest->testCase[testIdx]), 0x00, sizeof(struct FFTTestCase));
35  fftTest->testCase[testIdx].idx = testIdx;
36 
37  for(int poly_i = 0; poly_i<fftTest->polynomialLength; poly_i++){
38  for(int i=0; i<4; i++){
39  strncpy(tmp, line, 16); tmp[16]='\0';
40  fftTest->testCase[testIdx].polynomial[poly_i].word[3-i] = strtoul(tmp, NULL, 16);
41  line+=16*sizeof(char);
42  }
43 
44  line+=1*sizeof(char);
45  }
46 }
47 
48 void parsefft(char *line, ffttest_t *fftTest, unsigned char input){
49  char tmp[17]; uint64_t val;
50  int testIdx = fftTest->nTest;
51  line = memchr(line, ' ', 32)+sizeof(char);
52  for(int fft_i = 0; fft_i<fftTest->polynomialLength*2; fft_i++){
53  for(int i=0; i<12; i++){
54  strncpy(tmp, line, 16); tmp[16]='\0';
55  val = strtoul(tmp, NULL, 16);
56  if(input)
57  fftTest->testCase[testIdx].fftInput[fft_i].word[i] = val;
58  else
59  fftTest->testCase[testIdx].fftOutput[fft_i].word[i] = val;
60  line+=16*sizeof(char);
61  }
62  // Remove flag bits from coordinates
63  if(input)
64  fftTest->testCase[testIdx].fftInput[fft_i].word[11] &= (1ULL << 61) - 1;
65  else
66  fftTest->testCase[testIdx].fftOutput[fft_i].word[11] &= (1ULL << 61) - 1;
67 
68  line+=1*sizeof(char);
69  }
70 }
71 
72 void parsefftInput(char *line, ffttest_t *fftTest){
73  //fprintf(stderr, "<%s>\n", __func__);
74  if(strncmp(line, "fftTestInput", 12) != 0){
75  printf("Fatal, malformed input: Expected fftTestInput keyword\n");
76  exit(1);
77  }
78 
79  parsefft(line, fftTest, 1);
80 
81 }
82 
83 void parsefftOutput(char *line, ffttest_t *fftTest){
84  //fprintf(stderr, "<%s>\n", __func__);
85  if(strncmp(line, "fftTestOutput", 13) != 0){
86  printf("Fatal, malformed input: Expected fftTestInput keyword\n");
87  exit(1);
88  }
89 
90  parsefft(line, fftTest, 0);
91 
92 }
93 
94 void parseSetup(char *line, ffttest_t *fftTest){
95  //fprintf(stderr, "<%s>\n", __func__);
96  char tmp[32];
97  if(strncmp(line, "setup ", 6) != 0){
98  printf("Fatal, setup malformed >> %s", line);
99  exit(1);
100  }
101  line += 6*sizeof(char);
102  //printf("%s\n", line);
103 
104  for(int i=0; i<4; i++){
105  strncpy(tmp, line, 16); tmp[16]='\0';
106  fftTest->setup.word[3-i] = strtoul(tmp, NULL, 16);
107  line+=16*sizeof(char);
108  }
109 
110  //fprintf(stderr, "%lx %lx %lx %lx\n", fftTest->setup.word[0], fftTest->setup.word[1], fftTest->setup.word[2], fftTest->setup.word[3] );
111 }
112 
114  //gets out information on the read test, for debugging
115  #define eprintf(fmt , ...) fprintf(stderr, fmt, ##__VA_ARGS__)
116 
117  eprintf("Tests read: %d \n", test.nTest);
118  eprintf("Lenght of the polynomials: %d \n", test.polynomialLength);
119  eprintf("Setup %016lx %016lx %016lx %016lx \n", test.setup.word[0], test.setup.word[1], test.setup.word[2], test.setup.word[3]);
120  eprintf("\n");
121 
122  for(int i=0; i<test.nTest; i++){
123  eprintf("test %02d \n", i);
124  for(int ii=0; ii<4; ii++){
125  eprintf(">tfftin[%d] ", ii);
126  for (int j=0; j<6; j++) eprintf("%016lx ", test.testCase[i].fftInput[ii].word[j]); eprintf("\n");
127  eprintf(">>fftot[%d] ", ii);
128  for (int j=0; j<6; j++) eprintf("%016lx ", test.testCase[i].fftOutput[ii].word[j]); eprintf("\n");
129  }
130  }
131 
132  eprintf("\n");
133 }
134 
135 
136 ffttest_t parseFFTTest(const char *filename){
137  FILE *fp;
138  fp = fopen(filename, "r");
139  if (fp == NULL){
140  printf("Error while trying to open file %s\n", filename);
141  }
142  int ntests;
143  char *line = NULL;
144  size_t line_buff_size = 0;
145  int line_count=0;
146  ssize_t line_read_size = 0; //will infinite loop if this variable is not signed.
147 
148  //read first Line, expexted number of tests
149  line_read_size = getline(&line, &line_buff_size, fp);
150  ntests = atoi(line+7*sizeof(char));
151 
152  ffttest_t fftTest;
153  fftTest.nTest=0; fftTest.polynomialLength=POLYLEN;
154  fftTest.testCase = malloc(sizeof(struct FFTTestCase)*ntests);
155 
156  //read line, expected to be the setup
157  line_read_size = getline(&line, &line_buff_size, fp);
158  //printf("Read %ld chars in a %ld buff: %s", line_read_size, line_buff_size, line);
159  parseSetup(line, &fftTest);
160 
161  do{
162  line_read_size = getline(&line, &line_buff_size, fp);
163 
164  //stop reading at a hashmark
165  if (line[0]=='#')
166  break;
167  if(line_read_size < 0)
168  break;
169 
170  parsePoly(line, &fftTest, line_read_size);
171 
172  line_read_size = getline(&line, &line_buff_size, fp);
173  if(line_read_size < 0){
174  printf("fatal: No fftTestInput for Poly");
175  }
176 
177  parsefftInput(line, &fftTest);
178 
179  line_read_size = getline(&line, &line_buff_size, fp);
180  if(line_read_size < 0){
181  printf("fatal: No fftTestOutput for Poly");
182  }
183  parsefftOutput(line, &fftTest);
184 
185  fftTest.nTest++;
186  }while(1);
187 
188  fclose(fp);
189 
190  return fftTest;
191 }
192 
193 #ifdef PARSEDEBUG
194 void main(){
195  ffttest_t t = parseFFTTest("testFFT.in");
196  printTest(t);
197 }
198 #endif
199 // compile for test
200 // gcc -o parseFFTTest.elf parseFFTTest.c -std=c99 -ggdb -g3 -DPARSEDEBUG; ./parseFFTTest.elf
int main()
Definition: fftTest.cu:117
def test
Definition: test_fft.py:76
void parsefft(char *line, ffttest_t *fftTest, unsigned char input)
Definition: parseFFTTest.c:48
void parseSetup(char *line, ffttest_t *fftTest)
Definition: parseFFTTest.c:94
void freeffttest_t(ffttest_t *fftTest)
Definition: parseFFTTest.c:9
void parsefftInput(char *line, ffttest_t *fftTest)
Definition: parseFFTTest.c:72
ffttest_t parseFFTTest(const char *filename)
Definition: parseFFTTest.c:136
void parsefftOutput(char *line, ffttest_t *fftTest)
Definition: parseFFTTest.c:83
void parsePoly(char *line, ffttest_t *fftTest, ssize_t line_read_size)
Definition: parseFFTTest.c:16
#define eprintf(fmt,...)
void printTest(ffttest_t test)
Definition: parseFFTTest.c:113
#define POLYLEN
Definition: parseFFTTest.h:8
uint768_t fftOutput[POLYLEN *2]
Definition: parseFFTTest.h:14
uint256_t polynomial[POLYLEN]
Definition: parseFFTTest.h:12
unsigned int idx
Definition: parseFFTTest.h:11
uint768_t fftInput[POLYLEN *2]
Definition: parseFFTTest.h:13
unsigned int nTest
Definition: parseFFTTest.h:18
struct FFTTestCase * testCase
Definition: parseFFTTest.h:21
uint256_t setup
Definition: parseFFTTest.h:20
unsigned int polynomialLength
Definition: parseFFTTest.h:19
uint64_t word[4]
Definition: parseFFTTest.h:6
uint64_t word[12]
Definition: parseFFTTest.h:5