Skip to content

Commit

Permalink
Adding material of OpenACC/OpenMP talk
Browse files Browse the repository at this point in the history
  • Loading branch information
stammler committed Nov 26, 2024
1 parent e1f3c69 commit 81296ae
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
Binary file not shown.
70 changes: 70 additions & 0 deletions presentations/2024_11_25_openacc_openmp/mandelbrot.c
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);
}


25 changes: 25 additions & 0 deletions presentations/2024_11_25_openacc_openmp/test_openacc.c
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;


}
23 changes: 23 additions & 0 deletions presentations/2024_11_25_openacc_openmp/test_openmp.c
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;
}

0 comments on commit 81296ae

Please sign in to comment.