Skip to content

Commit

Permalink
More cleanup of OpenMP accelerator code
Browse files Browse the repository at this point in the history
  • Loading branch information
brobey committed Oct 16, 2019
1 parent 387fdcb commit 49dfc3d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 35 deletions.
5 changes: 5 additions & 0 deletions OpenMP/StreamTriad/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ add_executable(StreamTriad_par3 StreamTriad_par3.c timer.c timer.h)
set_target_properties(StreamTriad_par3 PROPERTIES COMPILE_FLAGS ${OpenMPAccel_C_FLAGS})
set_target_properties(StreamTriad_par3 PROPERTIES LINK_FLAGS "${OpenMPAccel_C_FLAGS}")

# Adds build target of stream_triad_par4 with source code files
add_executable(StreamTriad_par4 StreamTriad_par4.c timer.c timer.h)
set_target_properties(StreamTriad_par4 PROPERTIES COMPILE_FLAGS ${OpenMPAccel_C_FLAGS})
set_target_properties(StreamTriad_par4 PROPERTIES LINK_FLAGS "${OpenMPAccel_C_FLAGS}")

# Cleanup
add_custom_target(distclean COMMAND rm -rf CMakeCache.txt CMakeFiles
Makefile cmake_install.cmake StreamTriad.dSYM ipo_out.optrpt)
10 changes: 0 additions & 10 deletions OpenMP/StreamTriad/StreamTriad_par1.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,8 @@ int main(int argc, char *argv[]){
time_sum += cpu_timer_stop(tstart);
}


printf("Average runtime for stream triad loop is %lf msecs\n", time_sum/ntimes);

for (int k=0; k<ntimes; k++){
cpu_timer_start(&tstart);
// stream triad function call
StreamTriad(c, a, b, scalar, nsize);
time_sum += cpu_timer_stop(tstart);
}

printf("Average runtime for stream triad function call is %lf msecs\n", time_sum/ntimes);

free(a);
free(b);
free(c);
Expand Down
31 changes: 16 additions & 15 deletions OpenMP/StreamTriad/StreamTriad_par2.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,35 @@ int main(int argc, char *argv[]){
double *b = malloc(nsize * sizeof(double));
double *c = malloc(nsize * sizeof(double));

#pragma omp target enter data map(to:a[0:nsize], b[0:nsize], c[0:nsize])

struct timespec tstart;
// initializing data and arrays
double scalar = 3.0, time_sum = 0.0;

#pragma omp target data map(to:a[0:nsize], b[0:nsize], c[0:nsize])
{

#pragma omp target
#pragma omp teams distribute parallel for
for (int i=0; i<nsize; i++) {
a[i] = 1.0;
b[i] = 2.0;
}
for (int i=0; i<nsize; i++) {
a[i] = 1.0;
b[i] = 2.0;
}

for (int k=0; k<ntimes; k++){
cpu_timer_start(&tstart);
// stream triad loop
for (int k=0; k<ntimes; k++){
cpu_timer_start(&tstart);
// stream triad loop
#pragma omp target
#pragma omp teams distribute parallel for
for (int i=0; i<nsize; i++){
c[i] = a[i] + scalar*b[i];
for (int i=0; i<nsize; i++){
c[i] = a[i] + scalar*b[i];
}
time_sum += cpu_timer_stop(tstart);
}
time_sum += cpu_timer_stop(tstart);
}

} // #pragma omp target data map(from:c[0:nsize])

printf("Average runtime for stream triad loop is %lf msecs\n", time_sum/ntimes);

#pragma omp target exit data map(from:c[0:nsize])

free(a);
free(b);
free(c);
Expand Down
18 changes: 8 additions & 10 deletions OpenMP/StreamTriad/StreamTriad_par3.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ void StreamTriad(double *restrict c, double *restrict a, double *restrict b, dou
int main(int argc, char *argv[]){

int nsize = 20000000, ntimes=16;
//double *a = malloc(nsize * sizeof(double));
//double *b = malloc(nsize * sizeof(double));
//double *c = malloc(nsize * sizeof(double));
double *a, *b, *c;
double *a = malloc(nsize * sizeof(double));
double *b = malloc(nsize * sizeof(double));
double *c = malloc(nsize * sizeof(double));

#pragma omp target enter data map(alloc:a[0:nsize], b[0:nsize], c[0:nsize])
#pragma omp target enter data map(to:a[0:nsize], b[0:nsize], c[0:nsize])

struct timespec tstart;
// initializing data and arrays
Expand All @@ -36,14 +35,13 @@ int main(int argc, char *argv[]){
time_sum += cpu_timer_stop(tstart);
}


printf("Average runtime for stream triad loop is %lf msecs\n", time_sum/ntimes);

#pragma omp target exit data map(delete:a[0:nsize], b[0:nsize], c[0:nsize])
#pragma omp target exit data map(from:a[0:nsize], b[0:nsize], c[0:nsize])

//free(a);
//free(b);
//free(c);
free(a);
free(b);
free(c);

return(0);
}
Expand Down
47 changes: 47 additions & 0 deletions OpenMP/StreamTriad/StreamTriad_par4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "timer.h"

void StreamTriad(double *restrict c, double *restrict a, double *restrict b, double scalar, int nsize);

int main(int argc, char *argv[]){

int nsize = 20000000, ntimes=16;
double *a, *b, *c;

#pragma omp target enter data map(alloc:a[0:nsize], b[0:nsize], c[0:nsize])

struct timespec tstart;
// initializing data and arrays
double scalar = 3.0, time_sum = 0.0;
#pragma omp target
#pragma omp teams distribute parallel for
for (int i=0; i<nsize; i++) {
a[i] = 1.0;
b[i] = 2.0;
}

for (int k=0; k<ntimes; k++){
cpu_timer_start(&tstart);
// stream triad loop
#pragma omp target
#pragma omp teams distribute parallel for
for (int i=0; i<nsize; i++){
c[i] = a[i] + scalar*b[i];
}
time_sum += cpu_timer_stop(tstart);
}

printf("Average runtime for stream triad loop is %lf msecs\n", time_sum/ntimes);

#pragma omp target exit data map(delete:a[0:nsize], b[0:nsize], c[0:nsize])

return(0);
}

void StreamTriad(double *restrict c, double *restrict a, double *restrict b, double scalar, int nsize){
for (int i=0; i<nsize; i++){
c[i] = a[i] + scalar*b[i];
}
}

0 comments on commit 49dfc3d

Please sign in to comment.