diff options
| -rw-r--r-- | charf.c | 83 |
1 files changed, 42 insertions, 41 deletions
@@ -4,8 +4,6 @@ //for multithreading #include <pthread.h> #include <assert.h> -//for sleep -#include <unistd.h> #define DOUBLEMODE 0 #define DOUBLEERRORMODE 1 @@ -32,7 +30,7 @@ #endif /*length of the support of f*/ -static const int N=24; +static const int N=16; /*order of the derivative to consider.*/ static const int K=3; @@ -120,16 +118,7 @@ void compute_maximalfunction(VALUETYPE* f, VALUETYPE* Mf, int D){ } } -void compute(EXPTYPE p, int t, int D){ - /*allocate memory for f*/ - VALUETYPE f[N]; - /*Initiate f to be zero everywhere.*/ - for(int i=0; i<D; i++) f[i] = convert_int(0); - //for(int i=0; i<N; i++) f[N+i] = rand() %2; - /*In the middle of the domain set f to the values encoded in bit string t*/ - for(int i=0; i<D; i++) f[i] = convert_int((t >> i) & 1); - //if(i%3==0) f[2*N+i+K/2] = 1; - +void compute(EXPTYPE p, int D, VALUETYPE* f){ VALUETYPE Mf[N]; compute_maximalfunction(f,Mf,D); @@ -178,50 +167,62 @@ void compute(EXPTYPE p, int t, int D){ } } -struct Args {EXPTYPE* p; int* D; int* t; }; - -void* perform_work(void* arguments){ - struct Args *args = arguments; - EXPTYPE p = *(args->p); - int* D = args->D; - int* s = args->t; - - while(*D <= N){ - if(*s <= (1 << *D)-2){ - int t = *s; - *s = t+1; - compute(p,t,*D); +/*Writes into f the values of the function indexed by i. Returns the size of the support of f, or -1 if there is no function with that index.*/ +int generate_function(VALUETYPE* f, int i){ + int s=0; + int d=1; + while(d <= N){ + /*number of strings of length d that are not all 0s or all 1s*/ + int powd = (1<<d)-2; + if(i-s >= powd){ + s += powd; + d++; } else { - *s = 0; - *D = *D+1; + int t = i-s+1; + /*Set f to the values encoded in bit string t which is a value between 1 and powd = (1<<d)-2.*/ + for(int n=0; n<d; n++) f[n] = convert_int((t >> n) & 1); + return d; } } - return NULL; + return -1; + } -int main() { - /*exponent p of the L^p norm to consider*/ - EXPTYPE p = to_exptype(1); +typedef struct { + EXPTYPE p; + VALUETYPE** f; + int i; +}Args; - /*Iterate over all strings of 0s and 1s with length N. Those will represent f.*/ - int t = 1; - int D = 1; +/*Goes through all functions indexed by those numbers which equal the thread number module NUM_THREADS.*/ +void* compute_chunk(void* arguments){ + Args *args = arguments; + EXPTYPE p = args->p; + int i = args->i; - //for(int t=1; t<=(1 << N)-1; t++){ - //compute(N,K,D,p,t); - //} + VALUETYPE f[N]; + int d = generate_function(f,i); + while(d >= 0){ + compute(p,d,f); + i += NUM_THREADS; + d = generate_function(f,i); + } +} - struct Args args = {&p, &D, &t}; +int main() { + /*exponent p of the L^p norm to consider*/ + EXPTYPE p = to_exptype(1); pthread_t threads[NUM_THREADS]; + Args args[NUM_THREADS]; int result_code; for (int i = 0; i < NUM_THREADS; i++) { printf("In main: Creating thread %d.\n", i); - result_code = pthread_create(&threads[i], NULL, perform_work, &args); + args[i] = (Args){.p=p, .i=i}; + result_code = pthread_create(&threads[i], NULL, compute_chunk, &(args[i])); assert(!result_code); - usleep(1000*100); } // wait for each thread to complete |
