-
Notifications
You must be signed in to change notification settings - Fork 0
/
omp_solved4.c
49 lines (41 loc) · 1.48 KB
/
omp_solved4.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
41
42
43
44
45
46
47
48
/******************************************************************************
* FILE: omp_bug4.c
* DESCRIPTION:
* This very simple program causes a segmentation fault.
* AUTHOR: Blaise Barney 01/09/04
* LAST REVISED: 04/06/05
*
* BUG FOUND: The cache is too small for each thread to have their own copy
* of the matrix a, since a is 1048x1048 doubles (~800 MB). I've changed
* a to be a public variable so that it's shared and split up the work of
* filling in a amongst the threads using a parallel for.
*
******************************************************************************/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define N 1048
int main (int argc, char *argv[])
{
int nthreads, tid, i, j;
double a[N][N];
/* Fork a team of threads with explicit variable scoping */
#pragma omp parallel shared(nthreads, a) private(i,j,tid)
{
/* Obtain/print thread info */
tid = omp_get_thread_num();
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
printf("Thread %d starting...\n", tid);
/* Each thread works on its own private copy of the array */
#pragma omp for
for (i=0; i<N; i++)
for (j=0; j<N; j++)
a[i][j] = tid + i + j;
/* For confirmation */
printf("Thread %d done. Last element= %f\n",tid,a[N-1][N-1]);
} /* All threads join master thread and disband */
}