This page was last updated: February 16, 2024
A great use for parallel programming is identical operations on large arrays of numbers.
S = (Execution time with one thread) / (Execution time with four threads) = (Performance with four threads) / (Performance with one thread)
This number should be greater than 1.0 . If not, be sure you are using the correct numerator and denominator.
float Fp = (4./3.)*( 1. - (1./S) );Don't worry what this means just yet. This will become more meaningful soon.
Your main program would then look something like this:
#include <omp.h> #include <stdio.h> #include <math.h> #include <stdlib.h> #ifndef NUMT #define NUMT 1 // number of threads to use -- do once for 1 and once for 4 #endif #ifndef SIZE #define SIZE ?? // array size -- you get to decide #endif #define NUMTRIES 20 // how many times to run the timing to get reliable timing data float A[SIZE]; float B[SIZE]; float C[SIZE]; int main( ) { #ifdef _OPENMP fprintf( stderr, "OpenMP version %d is supported here\n", _OPENMP ); #else fprintf( stderr, "OpenMP is not supported here - sorry!\n" ); exit( 0 ); #endif // initialize the arrays: for( int i = 0; i < SIZE; i++ ) { A[i] = 1.; B[i] = 2.; } omp_set_num_threads( NUMT ); double maxMegaMults = 0.; for( int t = 0; t < NUMTRIES; t++ ) { double time0 = omp_get_wtime( ); #pragma omp parallel for for( int i = 0; i < SIZE; i++ ) { C[i] = A[i] * B[i]; } double time1 = omp_get_wtime( ); double megaMults = (double)SIZE/(time1-time0)/1000000.; if( megaMults > maxMegaMults ) maxMegaMults = megaMults; } fprintf( stderr, "For %d threads, Peak Performance = %8.2lf MegaMults/Sec\n", NUMT, maxMegaMults ); // note: %lf stands for "long float", which is how printf prints a "double" // %d stands for "decimal integer", not "double" // Speedup = (Peak performance for 4 threads) / (Peak performance for 1 thread) return 0; }
Feature | Points |
---|---|
Performance or Execution time results for 1 thread | 5 |
Performance or Execution time results for 4 threads | 5 |
One-thread-to-four-threads Speedup (>1.) | 5 |
Parallel Fraction | 10 |
Commentary | 5 |
Potential Total | 30 |