This repository has been archived on 2022-10-18. You can view files and clone it, but cannot push or open issues or pull requests.
HPC/Project2/Project2-code/bugs/omp_bug4.c

37 lines
1 KiB
C
Raw Normal View History

2022-10-18 12:39:50 +00:00
/******************************************************************************
* FILE: omp_bug4.c
* DESCRIPTION:
* This very simple program causes a segmentation fault.
******************************************************************************/
#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) private(i, j, tid, a)
{
/* 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 */
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 */
}