summaryrefslogtreecommitdiff
path: root/charf.c
diff options
context:
space:
mode:
authorJulian Weigt <juw@posteo.de>2025-12-23 19:27:46 +0000
committerJulian Weigt <juw@posteo.de>2026-02-04 15:55:46 +0100
commitf3129294bf92daae6ca72e2f8c456bdff97b8b3e (patch)
treefd2dd52d1f98181d12eafc98e382cedcabe2f47c /charf.c
parentb43f9d38196080333680681766b6f23f024c0cb0 (diff)
Enable multithreading.
Diffstat (limited to 'charf.c')
-rw-r--r--charf.c62
1 files changed, 53 insertions, 9 deletions
diff --git a/charf.c b/charf.c
index a2d1ad8..f52d41e 100644
--- a/charf.c
+++ b/charf.c
@@ -2,11 +2,14 @@
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
+#include <assert.h>
#ifndef EXACT
#define EXACT false
#endif
+#define NUM_THREADS 4
+
#if EXACT
#include "ratio.h"
#define VALUETYPE rational
@@ -106,8 +109,8 @@ void compute(int N, int K, int D, EXPTYPE p, int t){
//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<N;i++){
- /*Since we care about the Kth derivative, which in i depends on f on [i,i+K], shift f to the right by K/2 so that the most interesting part of f^{(K)} and Mf^{(K)} will be around the center of the domain*/
- f[(D-N)/2+i+K/2] = convert_int((t >> i) & 1);
+ /*Since we care about the Kth derivative, which in i depends on f on [i,i+K], shift f to the right by K so that the support of f^{(K)} stays within the domain.*/
+ f[(D-(N+K))/2+K+i] = convert_int((t >> i) & 1);
//if(i%3==0) f[2*N+i+K/2] = 1;
}
@@ -147,8 +150,9 @@ void compute(int N, int K, int D, EXPTYPE p, int t){
//printf("%.3d: %.3f \n",t,r);
/*Print f and ||Mf^{(k)}||_p/||f^{(k)}||_p if the latter is close to 1/2.*/
- if(to_double(r)>.4997){
- //if(to_double(r)>.7)
+ //if(to_double(r)>.4997)
+ if(to_double(r)>.6)
+ {
printf("f: ");
for(int i=0;i<D;i++) printf("%1.0f ",to_double(f[i]));
printf("\n");
@@ -158,21 +162,61 @@ void compute(int N, int K, int D, EXPTYPE p, int t){
free(Mf);
}
+struct Args {int* N; int* K; int* D; EXPTYPE* p; int* t; };
+
+void* perform_work(void* arguments){
+ struct Args *args = arguments;
+ int N = *(args->N);
+ int K = *(args->K);
+ int D = *(args->D);
+ EXPTYPE p = *(args->p);
+ int* s = args->t;
+
+ while(*s <= (1 << N)-1){
+ int t = *s;
+ *s = t+1;
+ compute(N,K,D,p,t);
+ }
+ return NULL;
+}
+
int main() {
/*length of the support of f*/
- int N=12;
+ int N=16;
/*order of the derivative to consider. Should not be larger than (D-N)/2 because then the support of f^{(K)} reaches outside of our domain.*/
- int K=3;
+ int K=10;
- /*length of the domain*/
- int D=N+K+1;
+ /*length of the domain, minimum N+K to make sure the support of f^{(k)} belongs to the domain*/
+ int D=N+K+N/2;
/*exponent p of the L^p norm to consider*/
EXPTYPE p = 1;
+ int t = 1;
/*Iterate over all strings of 0s and 1s with length N. Those will represent f.*/
- for(int t=1; t<=(1 << N)-1; t++) compute(N,K,D,p,t);
+
+ //for(int t=1; t<=(1 << N)-1; t++){
+ //compute(N,K,D,p,t);
+ //}
+
+ struct Args args = {&N, &K, &D, &p, &t};
+
+ pthread_t threads[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);
+ assert(!result_code);
+ }
+
+ // wait for each thread to complete
+ for (int i = 0; i < NUM_THREADS; i++) {
+ result_code = pthread_join(threads[i], NULL);
+ assert(!result_code);
+ printf("In main: Thread %d has ended.\n", i);
+ }
return 0;
}