cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

Meteorhead
Challenger

OpenMP problem

somone please help

Hi!

I'm learning OpenMP, and I have a problem which I cannot solve. I'm developing a simple application to learn and test the basics, and the program seems crashing all the time for a reason unknown to me.

The code is attached, and I compile it with VS2010 on a 64-bit Win7 Ultimate. This very same code compiles and functions alright on an 64-bit Ubuntu 10.04.3

I run this on a quad-core machine, thus in the array sharing part, first output is: 10 10 11 11 12 12 13 13

I would expect second time around for the output to be: 10 11 12 13 14 15 16 17, but instead it crashes.

Could someone tell me why it crashes? Many thanks for any help.

#include <omp.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { // for(int i = 0 ; i < 64 ; ++i) printf("%d ", (int)(floor((double)rand()/RAND_MAX*2*omp_get_max_threads()))); // This statement should only print once printf("Starting Program!\n"); printf("Maximum concurrent threads available: %d\n", omp_get_max_threads()); printf("Setting deafult accordingly...\n\n"); omp_set_num_threads(omp_get_max_threads()); printf("Default branching here:\nFORK\n"); #pragma omp parallel { printf("Executed by thread # %d \n", omp_get_thread_num()); } printf("JOIN\n\n"); printf("Forced branching here:\nFORK\n"); #pragma omp parallel num_threads(8) { printf("Executed by thread # %d \n", omp_get_thread_num()); } printf("JOIN\n\n"); int* a = (int*)malloc( 2*omp_get_max_threads()*sizeof(int)); int sum = 0; printf("Implicit sharing of array:\nFORK\n"); #pragma omp parallel num_threads(2*omp_get_max_threads()) { for(int i = 0 ; i < omp_get_num_threads() / omp_get_max_threads() ; ++i) { a[omp_get_thread_num()*2+i] = 10 + omp_get_thread_num(); } } printf("JOIN\n"); for(int i = 0 ; i < 2*omp_get_max_threads() ; ++i) {printf("%d ", a); a = 0;} printf("\n\n"); printf("Implicit sharing of array again:\nFORK\n"); #pragma omp parallel { for(int i = 0 ; i < omp_get_num_threads() / omp_get_max_threads() ; ++i) { a[omp_get_thread_num()] = 10 + omp_get_thread_num(); } } printf("JOIN\n"); for(int i = 0 ; i < 2*omp_get_max_threads() ; ++i) {printf("%d ", a); a = 0;} printf("\n\n"); }

0 Likes
0 Replies