Created
October 26, 2009 11:23
-
-
Save kidd/218594 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ===================================================================================== | |
* Filename: hello.c | |
* Author: Raimon Grau Cuscó (rg), raimonster@gmail.com | |
* ===================================================================================== | |
*/ | |
#include <omp.h> | |
#include <stdlib.h> | |
int main () | |
{ | |
double start,end, r, sh; | |
srand(8); | |
//srand(time(0)); | |
sh=0; | |
start = omp_get_wtime(); | |
printf("Total number of procs: %d\n", omp_get_num_procs()); | |
printf("Total number of max available threads: %d\n", omp_get_max_threads()); | |
#pragma omp parallel shared(sh) | |
{ | |
printf("Hello world! %d\n", omp_get_thread_num()); | |
sh+=rand(); | |
printf("%f\n", sh); | |
} | |
end = omp_get_wtime(); | |
printf("Elapsed time: %f\n", end - start); | |
printf("acc rand: %f\n",sh); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <math.h> | |
#include <omp.h> | |
#define MATSIZE 700 | |
#define DEBUG | |
void init_mat (double M[MATSIZE][MATSIZE], double v) | |
{ | |
int i, j; | |
#pragma omp for collapse(2) | |
for (i=0; i<MATSIZE; i++) { | |
for (j=0; j<MATSIZE; j++) { | |
M[i][j] = sin((double) i); | |
} | |
} | |
} | |
void matmul (double C[MATSIZE][MATSIZE], | |
double A[MATSIZE][MATSIZE], double B[MATSIZE][MATSIZE]) | |
{ | |
int i, j, k; | |
double aux; | |
#pragma omp for private(j,k,aux) | |
for (i=0; i<MATSIZE; i++) { | |
for (j=0; j<MATSIZE; j++) { | |
aux = 0; | |
for (k=0; k<MATSIZE; k++) { | |
aux += A[i][k] *B[k][j]; | |
} | |
C[i][j] += aux; | |
} | |
} | |
} | |
int comp (double A[MATSIZE][MATSIZE], double B[MATSIZE][MATSIZE]) | |
{ | |
int i, j, count = 0; | |
for (i=0; i<MATSIZE; i++) { | |
for (j=0; j<MATSIZE; j++) { | |
if (abs (A[i][j]- B[i][j]) > 1e-10) count++; | |
} | |
} | |
return count; | |
} | |
int main (int argc, char * argv[]) | |
{ | |
double start, end; | |
double A[MATSIZE][MATSIZE]; | |
double B[MATSIZE][MATSIZE]; | |
double C[MATSIZE][MATSIZE]; | |
init_mat (A, 0.1); | |
init_mat (B, 0.1); | |
init_mat (C, 0.0); | |
start = omp_get_wtime(); | |
matmul (C, A, B); | |
end = omp_get_wtime(); | |
printf ("matmul1 time: %f seconds\n", end-start); | |
#ifdef DEBUG | |
double D[MATSIZE][MATSIZE]; | |
init_mat (D, 0.0); | |
omp_set_num_threads(1); | |
matmul (D,A,B); | |
printf ("Comparing...\n"); | |
if (comp (C, D)) printf ("matmul unsuccessful\n"); | |
else printf ("matmul ok\n"); | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment