Skip to content

Instantly share code, notes, and snippets.

@xk
Last active May 2, 2017 14:49
Show Gist options
  • Save xk/ba76a426e4c391c0b22b3eddbdb11898 to your computer and use it in GitHub Desktop.
Save xk/ba76a426e4c391c0b22b3eddbdb11898 to your computer and use it in GitHub Desktop.
8 cores 96MB buffers
// 2017-04-30 jorge@jorgechamorro.com
// See http://www.eevblog.com/forum/projects/in-search-of-a-supercomputer
#include <pthread.h>
#include <stdlib.h>
#define BUF_SIZE 96*1024*1024
pthread_t t1, t2, t3, t4, t5, t6, t7, t8;
double* nu_buffer () { return (double*) malloc(BUF_SIZE); }
void* thread_proc (void* arg) {
int i= BUF_SIZE/8;
double* buffer= nu_buffer();
while (i--) buffer[i]= (((double*) arg)[i] + buffer[i]) / 2.0;
return buffer;
}
void do_threads_stuff (double* buffer) {
pthread_create(&t1, NULL, thread_proc, buffer);
pthread_create(&t2, NULL, thread_proc, buffer);
pthread_create(&t3, NULL, thread_proc, buffer);
pthread_create(&t4, NULL, thread_proc, buffer);
pthread_create(&t5, NULL, thread_proc, buffer);
pthread_create(&t6, NULL, thread_proc, buffer);
pthread_create(&t7, NULL, thread_proc, buffer);
pthread_create(&t8, NULL, thread_proc, buffer);
void* r;
pthread_join(t1, &r), free(r);
pthread_join(t2, &r), free(r);
pthread_join(t3, &r), free(r);
pthread_join(t4, &r), free(r);
pthread_join(t5, &r), free(r);
pthread_join(t6, &r), free(r);
pthread_join(t7, &r), free(r);
pthread_join(t8, &r), free(r);
}
int main (void) {
double* buffer= nu_buffer();
do_threads_stuff(buffer);
//free(buffer); return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment