diff --git a/PSA02/omp_sum_v1.c b/PSA02/omp_sum_v1.c new file mode 100644 index 0000000000000000000000000000000000000000..06dd84c59147113c033d12722a7595168b9f15cf --- /dev/null +++ b/PSA02/omp_sum_v1.c @@ -0,0 +1,49 @@ +#include <stdio.h> +#include <stdlib.h> +#include <omp.h> + +int main(int argc, char *argv[]) { + + /* get N and thread_count from command line */ + if (argc < 3) { + printf ("Command usage : %s %s %s\n",argv[0],"N","thread_count"); + return 1; + } + long long N = atol(argv[1]); + int thread_count = atoi(argv[2]); + omp_set_num_threads(thread_count); + + /* start the timer */ + double start_time, end_time; + start_time = omp_get_wtime(); + + /* calculate the thread sums in parallel */ + long long thread_sums[thread_count]; + +#pragma omp parallel default(none) shared(N,thread_sums) + { + int thread_num = omp_get_thread_num(); + thread_sums[thread_num] = 0; +#pragma omp for + for (long long i = 1; i <= N;i++) { + thread_sums[thread_num] += i; + } + } + + /* add the results of the thread_sums */ + long long sum = 0; + for (int i=0;i<thread_count;i++) { + sum += thread_sums[i]; + } + + /* stop the timer */ + end_time = omp_get_wtime(); + + /* print results */ + printf ("thread_count = %d, ",thread_count); + printf ("elapsed time = %g\n",end_time-start_time); + printf (" N = %llu, ",N); + printf ("sum = %llu, ",sum); + printf ("N*(N+1)/2 = %llu\n",(N/2)*(N+1)); + +} diff --git a/PSA02/omp_sum_v1.sh b/PSA02/omp_sum_v1.sh new file mode 100644 index 0000000000000000000000000000000000000000..02f656df3935ce32fce35a9dd62b20de9f5ef137 --- /dev/null +++ b/PSA02/omp_sum_v1.sh @@ -0,0 +1,24 @@ +#!/bin/bash +#SBATCH -A cmda3634_rjh +#SBATCH -p normal_q +#SBATCH -t 5 +#SBATCH --cpus-per-task=4 +#SBATCH -o omp_sum_v1.out + +# Go to the directory where the job was submitted +cd $SLURM_SUBMIT_DIR + +# Load the modules +module load matplotlib + +# Build the executable +gcc -o omp_sum_v1 omp_sum_v1.c -fopenmp + +# OpenMP settings +export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK +export OMP_PROC_BIND=true + +# run omp_sum +./omp_sum_v1 200000000 1 +./omp_sum_v1 200000000 2 +./omp_sum_v1 200000000 4 diff --git a/PSA02/omp_sum_v2.c b/PSA02/omp_sum_v2.c new file mode 100644 index 0000000000000000000000000000000000000000..6c6daefb17ea63bd56437901369bbab85f765867 --- /dev/null +++ b/PSA02/omp_sum_v2.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> +#include <omp.h> + +#define PAD_SIZE 128 +typedef struct thread_sum_s { + long long sum; + int pad[PAD_SIZE]; +} thread_sum_type; + +int main(int argc, char *argv[]) { + + /* get N and thread_count from command line */ + if (argc < 3) { + printf ("Command usage : %s %s %s\n",argv[0],"N","thread_count"); + return 1; + } + long long N = atol(argv[1]); + int thread_count = atoi(argv[2]); + omp_set_num_threads(thread_count); + + /* start the timer */ + double start_time, end_time; + start_time = omp_get_wtime(); + + /* calculate the thread sums in parallel */ + thread_sum_type thread_sums[thread_count]; + +#pragma omp parallel default(none) shared(N,thread_sums) + { + int thread_num = omp_get_thread_num(); + thread_sums[thread_num].sum = 0; +#pragma omp for + for (long long i = 1; i <= N;i++) { + thread_sums[thread_num].sum += i; + } + } + + /* add the results of the thread_sums */ + long long sum = 0; + for (int i=0;i<thread_count;i++) { + sum += thread_sums[i].sum; + } + + /* stop the timer */ + end_time = omp_get_wtime(); + + /* print results */ + printf ("thread_count = %d, ",thread_count); + printf ("elapsed time = %g\n",end_time-start_time); + printf (" N = %llu, ",N); + printf ("sum = %llu, ",sum); + printf ("N*(N+1)/2 = %llu\n",(N/2)*(N+1)); + +} diff --git a/PSA02/omp_sum_v2.sh b/PSA02/omp_sum_v2.sh new file mode 100644 index 0000000000000000000000000000000000000000..5e319360ed33d4b88cf912e80b60549086382540 --- /dev/null +++ b/PSA02/omp_sum_v2.sh @@ -0,0 +1,24 @@ +#!/bin/bash +#SBATCH -A cmda3634_rjh +#SBATCH -p normal_q +#SBATCH -t 5 +#SBATCH --cpus-per-task=4 +#SBATCH -o omp_sum_v2.out + +# Go to the directory where the job was submitted +cd $SLURM_SUBMIT_DIR + +# Load the modules +module load matplotlib + +# Build the executable +gcc -o omp_sum_v2 omp_sum_v2.c -fopenmp + +# OpenMP settings +export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK +export OMP_PROC_BIND=true + +# run omp_sum +./omp_sum_v2 200000000 1 +./omp_sum_v2 200000000 2 +./omp_sum_v2 200000000 4