Skip to content

Instantly share code, notes, and snippets.

@prehensilecode
Created March 30, 2017 21:36
Show Gist options
  • Save prehensilecode/2db81a8ae66d4b79d754c9232ab03804 to your computer and use it in GitHub Desktop.
Save prehensilecode/2db81a8ae66d4b79d754c9232ab03804 to your computer and use it in GitHub Desktop.
Dumb program to just allocate some memory in each thread using OpenMP
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#define K 1024l
#define MEG 1048576l
#define GIG 1073741824l
/***
**** Allocate some chunk of memory in each thread
***/
int
main (int argc, char *argv[])
{
size_t i, j;
const size_t blobsize = 512 * MEG;
/*** Start of parallel region ***/
#pragma omp parallel private(i, j)
{
/* sizeof(double) = 8 => allocate 4 GB per thread */
double *blob = (double*)NULL;
if ((double*)NULL == (blob = (double*)calloc(blobsize, sizeof(double))))
{
fprintf(stderr, "Error allocating blob in thread %d\n", omp_get_thread_num());
exit(1);
}
for (i = 0; i < 250; ++i)
{
for (j = 0; j < blobsize; ++j)
{
blob[j] = (double)j * (double)sqrt((double)j);
}
}
for (i = 0; i < 32; ++i)
{
printf("blob[%ld] = %g\n", i, blob[i]);
}
free(blob);
}
/*** End of parallel region ***/
sleep(90);
printf ("Done.\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment