-
Notifications
You must be signed in to change notification settings - Fork 2
/
openmp3.c
40 lines (39 loc) · 917 Bytes
/
openmp3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/***************************************************************/
#include <omp.h>
void ** ompx_calloc(size_t bytes)
{
int np = omp_get_max_threads();
void ** ptrs = malloc(np*sizeof(void*));
#pragma omp parallel shared(ptrs)
{
int me = omp_get_thread_num();
ptrs[me] = malloc(bytes);
memset(ptrs[me],0,bytes);
}
return ptrs;
}
void ompx_free(void ** ptrs)
{
#pragma omp parallel shared(ptrs)
{
int me = omp_get_thread_num();
free(ptrs[me]);
}
free(ptrs);
}
int main(int argc, char* argv[]) {
int n = (argc>1) ? atoi(argv[1]) : 1<<20;
int np = omp_get_max_threads();
if (np<2) exit(1);
int ** A = (int**)ompx_calloc(n*sizeof(int));
#pragma omp parallel shared(A)
{
/* threaded computation */
}
ompx_free((void**)A);
return 0;
}
/***************************************************************/