b’2021_Spring_Hw03_Part1.tar.gz’
CC=g++
CFLAGS= -O3 -pthread
DEPS =
OBJ = main.o calcSd.o
EXEC = sd_test
LIB = _sd_thread
LIB_DIR = abc123_threadSd
%.o: %.cpp $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS) -I. -I./$(LIB_DIR)
$(EXEC): $(OBJ)
module load gcc; \
cd $(LIB_DIR) && $(MAKE);
module load gcc; \
$(CC) -o $@ $^ $(CFLAGS) -L./$(LIB_DIR)/ -l$(LIB) -L.
.PHONY: clean
clean:
cd $(LIB_DIR) && $(MAKE) clean
rm -f $(OBJ) $(EXEC)
/**
* CMPCS-450 Assignment 3
* sd_thread.h
* Warning: Do not change anything in this file
*
* @author Niranjan Thirusangu, William Seisler
* @version 1.2 02/18/2020
*/
#ifndef SD_THREAD
#define SD_THREAD
// a structure containing the statistics of the dataset (self defined)
struct STDDEV_RESULT
{
double mean;
double min;
double max;
double stddev;
};
struct THRESH_RESULT
{
long int *pli_list; // an array containing indexes of
// values over threshold
long int li_threshCount; // size of pli_list
};
/**
Returns the mean, min, max and Standard deviation of a given array.
@param A Pointer to the first element of the A array.
@param N Number of elements in the A array.
@param P Number of threads the function should use.
@return mean, min, max and sd using result structure.
*/
STDDEV_RESULT* calcSdThread(double *A, long N, int P);
/**
Returns a list of indices of values over a threshold in a given array.
@param A Pointer to the first element of the A array.
@param N Number of elements in the A array.
@param P Number of threads the function should use.
@return threshold list and count in threshold structure.
*/
THRESH_RESULT *findThreshValuesThread(double *A, long N, double T, int P);
#endif
CC=g++
CFLAGS= -I. -O3
DEPS = sd_thread.h
OBJ = sd_thread.o
LIB = lib_sd_thread.a
%.o: %.cpp $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
$(LIB): $(OBJ)
ar rcs $@ $(OBJ)
.PHONY: clean
clean:
rm -f $(OBJ) $(LIB)
#include
#include
#include
#include
#include
#include “sd_thread.h”
using namespace std;
STDDEV_RESULT* calcSdThread(double *A, long N, int P)
{
struct STDDEV_RESULT* res = new STDDEV_RESULT;
double sd_temp, mean, min, max, sd;
min = RAND_MAX;
max = 0.0;
sd = 0;
sd_temp = 0;
mean = 0;
// perform the summation for the mean
for(long i = 0; i < N; i++)
{
mean = mean+A[i];
}
mean /= (double) N;
// perform the summation for the std_dev
for(long i = 0; i < N; i++)
{
sd_temp += (A[i] - mean) * (A[i] - mean);
}
sd=sqrt(sd_temp/(double)N);
// find min and max
for(long i = 0; i < N; i++)
{
if(max < A[i])
{
max = A[i];
}
if(min > A[i])
{
min = A[i];
}
}
// store off the values to return
res->mean = mean;
res->min = min;
res->max = max;
res->stddev = sd;
return res;
}
THRESH_RESULT *findThreshValuesThread(double *A, long N, double T, int P)
{
THRESH_RESULT *p_tmpResult = new THRESH_RESULT;
// traverse the list once to find the count of values over threshold
long c = 0;
for (long i=0; i < N; i++)
{
if (A[i] > T)
c++;
}
// store the count and allocate an array to store the results
p_tmpResult->li_threshCount = c;
p_tmpResult->pli_list = new long[c];
c = 0;
// store the index locations of the values over threshold
for (long i=0; i < N; i++){
if (A[i] > T){
p_tmpResult->pli_list[c] = i;
c++;
}
}
return p_tmpResult;
}
2021_Spring_Hw03_Part1/main.cpp
2021_Spring_Hw03_Part1/main.cpp/**
* CMPCS-450 Assignment 3
* main.cpp
* Warning: Do not change anything in this file
*
* @author Niranjan Thirusangu, William Seisler
* @version 1.2 02/18/2020
*/
#include
#include
#include
#include
#include
#include
#include “calcSd.h”
using namespace std;
void get_walltime(double* wcTime);
int main(int argc, char *argv[])
{
// some variables to track walltime for benchmark
double wcs, wce, serial_duration, thread_duration;
// a separate results object for each test
STDDEV_RESULT* res_serial;
STDDEV_RESULT* res_thread;
long N = 1000000000;
int P = 10;
double T = 999.0;
if (argc >= 2)
{ // 1st argument is N
N = atoi(argv[1]);
}
if (argc >= 3)
{ // 2nd argument is P
P = atoi(argv[2]);
}
if (argc >= 4)
{ // 3rd argument is T
T = atof(argv[3]);
}
printf(“N is set to: %d\n”,N);
printf(“P is set to: %d\n”,P);
printf(“T is set to: %0.5f\n”,T);
srand(time(0));
double *A = new double[N];
for(long i=0;i
(res_thread->min==res_serial->min?”passed”:”failed”) );
printf(“Max: %f, %f, Check %s \n”, res_thread->max, res_serial->max,
(res_thread->max==res_serial->max?”passed”:”failed”) );
printf(“Mean: %f, %f, Check %s \n”, res_thread->mean, res_serial->mean,
(res_thread->mean==res_serial->mean?”passed”:”failed”) );
printf(“Standard Deviation: %0.9f, %0.9f, Check %s\n”, res_thread->stddev, res_serial->stddev,
(res_thread->stddev==res_serial->stddev?”passed”:”failed”));
printf(“STD_DEV Wall times: OMP: %f, Serial: %f\n”, thread_duration, serial_duration);
THRESH_RESULT *p_serialThreshRes;
THRESH_RESULT *p_threadThreshRes;
// benchmark serial threshold implementation
get_walltime(&wcs);
p_serialThreshRes = findThreshValuesSerial(A, N, T);
get_walltime(&wce);
serial_duration = wce-wcs;
// benchmark threaded threshold implementation
get_walltime(&wcs);
p_threadThreshRes = findThreshValuesThread(A, N, T, P);
get_walltime(&wce);
thread_duration = wce-wcs;
printf(“Checking correctness of Thresholding \n”);
printf(“Threshold Count: %i, %i, Check %s\n”,
p_threadThreshRes->li_threshCount,
p_serialThreshRes->li_threshCount,
(p_threadThreshRes->li_threshCount==p_serialThreshRes->li_threshCount ? “passed”:”failed”));
for (long ii = 0; ii < p_serialThreshRes->li_threshCount; ii++)
{
if (A[p_serialThreshRes->pli_list[ii]] < T)
{
printf("Error! Serial Threshold list is inaccurate! Item %i failed! A[%i] = %f (%f)\n",
ii, p_serialThreshRes->pli_list[ii],
A[p_serialThreshRes->pli_list[ii]], T);
break;
}
}
for (long ii = 0; ii < p_threadThreshRes->li_threshCount; ii++)
{
if (A[p_threadThreshRes->pli_list[ii]] < T)
{
printf("Error! Thread Threshold list is inaccurate! Item %i failed! A[%i] = %f (%f)\n",
ii, p_threadThreshRes->pli_list[ii],
A[p_threadThreshRes->pli_list[ii]], T);
break;
}
}
printf(“THRESH Wall times: OMP: %f, Serial: %f\n”, thread_duration, serial_duration);
delete[] A;
delete res_serial;
delete res_thread;
delete[] p_threadThreshRes->pli_list;
delete p_threadThreshRes;
delete[] p_serialThreshRes->pli_list;
delete p_serialThreshRes;
return 0;
}
void get_walltime(double* wcTime)
{
struct timeval tp;
gettimeofday(&tp, NULL);
*wcTime = (double)(tp.tv_sec + tp.tv_usec/1000000.0);
}
2021_Spring_Hw03_Part1/calcSd.cpp
2021_Spring_Hw03_Part1/calcSd.cpp
#include
#include
#include
#include
using namespace std;
STDDEV_RESULT* calcSdSerial(double *A, long N)
{
struct STDDEV_RESULT* res = new STDDEV_RESULT;
double sd_temp, mean, min, max, sd;
min = RAND_MAX;
max = 0.0;
sd = 0;
sd_temp = 0;
mean = 0;
// perform the summation for the mean
for(long i = 0; i < N; i++)
{
mean = mean+A[i];
}
mean /= (double) N;
// perform the summation for the std_dev
for(long i = 0; i < N; i++)
{
sd_temp += (A[i] - mean) * (A[i] - mean);
}
sd=sqrt(sd_temp/(double)N);
// find min and max
for(long i = 0; i < N; i++)
{
if(max < A[i])
{
max = A[i];
}
if(min > A[i])
{
min = A[i];
}
}
// store off the values to return
res->mean = mean;
res->min = min;
res->max = max;
res->stddev = sd;
return res;
}
THRESH_RESULT *findThreshValuesSerial(double *A, long N, double T)
{
THRESH_RESULT *p_tmpResult = new THRESH_RESULT;
// traverse the list once to find the count of values over threshold
long c = 0;
for (long i=0; i < N; i++)
{
if (A[i] > T)
c++;
}
// store the count and allocate an array to store the results
p_tmpResult->li_threshCount = c;
p_tmpResult->pli_list = new long[c];
c = 0;
// store the index locations of the values over threshold
for (long i=0; i < N; i++){
if (A[i] > T){
p_tmpResult->pli_list[c] = i;
c++;
}
}
return p_tmpResult;
}
#include “sd_thread.h”
STDDEV_RESULT* calcSdSerial(double *A, long N);
THRESH_RESULT *findThreshValuesSerial(double *A, long N, double T);
2021_Spring_Hw03_Part1/Makefile
2021_Spring_Hw03_Part1/abc123_threadSd/sd_thread.h
2021_Spring_Hw03_Part1/abc123_threadSd/Makefile
2021_Spring_Hw03_Part1/abc123_threadSd/sd_thread.cpp
2021_Spring_Hw03_Part1/main.cpp
2021_Spring_Hw03_Part1/calcSd.cpp
2021_Spring_Hw03_Part1/calcSd.h