-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding material of OpenACC/OpenMP talk
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <omp.h> | ||
#include <openacc.h> | ||
|
||
#define MAX_ITER 10000 | ||
#define MAX_PALETTE 100 | ||
#define f 1 | ||
|
||
static const int Nx = 1920 * f; | ||
static const int Ny = 1080 * f; | ||
|
||
int mandelbrot(int i, int j, int Nx, int Ny); | ||
void write_to_file(int* M); | ||
|
||
int main(int argc, char* argv[]){ | ||
|
||
int* M = calloc (Nx * Ny, sizeof(int)); | ||
|
||
//#pragma acc parallel loop // Use OpenACC to parallelize the for loops | ||
//#pragma omp target parallel for map(to: Nx, Ny) map(from: M[0: Nx*Ny]) // Use OpenMP to parallelize the for loops | ||
for(int j = 0; j < Ny; j++){ | ||
for(int i = 0; i < Nx; i++){ | ||
M[j * Nx + i] = mandelbrot(i, j, Nx, Ny) % MAX_PALETTE; | ||
} | ||
} | ||
|
||
//write_to_file(M); // Comment out to just check the performances | ||
|
||
return 0; | ||
|
||
} | ||
|
||
int mandelbrot(int i, int j, int Nx, int Ny){ | ||
static const double x_L = -1.7687784; | ||
static const double x_R = -1.7687792; | ||
static const double y_L = -0.0017386; | ||
static const double y_R = -0.0017392; | ||
|
||
double px = x_L + i * (x_R - x_L) / Nx; | ||
double py = y_L + j * (y_R - y_L) / Ny; | ||
|
||
double xc = px; | ||
double temp_xc = xc; | ||
double yc = py; | ||
short int iter = 0; | ||
while(xc*xc + yc*yc < 4 && iter < MAX_ITER){ | ||
temp_xc = xc*xc - yc*yc + px; | ||
yc = 2.*xc*yc + py; | ||
xc = temp_xc; | ||
|
||
iter++; | ||
} | ||
return iter; | ||
} | ||
|
||
|
||
void write_to_file(int* M){ | ||
FILE *file = fopen("matrix.txt", "w"); | ||
for (int j = 0; j < Ny; j++) { | ||
for (int i = 0; i < Nx; i++) { | ||
|
||
fprintf(file, "%d ", M[j * Nx + i]); | ||
} | ||
fprintf(file, "\n"); // Newline after each row | ||
} | ||
fclose(file); | ||
} | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <openacc.h> | ||
|
||
/*************************************************/ | ||
/** HOW TO COMPILE: nvc -acc test_openacc.c **/ | ||
/*************************************************/ | ||
|
||
int main(){ | ||
|
||
#pragma acc parallel // Open a parallel region with OpenACC. Whatever is inside the brackets will be run on the GPU | ||
{ | ||
if(acc_on_device(acc_device_default)){ // Function provided by OpenACC to check whether a code is being run on the GPU (the device) | ||
printf("Running on GPU\n"); | ||
}else{ | ||
printf("Running on CPU\n"); | ||
} | ||
} // acc parallel | ||
|
||
sleep(10); // Wait for 10 seconds, to check that nvidia-smi shows that a code is running on the GPU | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <omp.h> | ||
|
||
/***************************************************/ | ||
/** HOW TO COMPILE: nvc -mp=gpu test_openmp.c **/ | ||
/***************************************************/ | ||
|
||
int main(){ | ||
|
||
#pragma omp target // Open a parallel region with OpenMP. Whatever is inside the brackets will be run on the GPU | ||
{ | ||
if(!omp_is_initial_device()){ // Function provided by OpenMP to check whether a code is being run on the GPU or on the CPU (the "initial_device") | ||
printf("Running on GPU\n"); | ||
}else{ | ||
printf("Running on CPU\n"); | ||
} | ||
} // omp target | ||
|
||
sleep(10); // Wait for 10 seconds, to check that nvidia-smi shows that a code is running on the GPU | ||
|
||
return 0; | ||
} |