Merge changes

This commit is contained in:
Michael Boulton 2015-07-28 11:37:20 +01:00
parent bb0dcce28b
commit b43eb9cf16
4 changed files with 485 additions and 13654 deletions

View File

@ -1,6 +1,5 @@
LIBS = -l OpenCL
FLAGS = -std=c++11 -O3
LDLIBS = -l OpenCL
CXXFLAGS = -std=c++11 -O3
PLATFORM = $(shell uname -s)
ifeq ($(PLATFORM), Darwin)
@ -9,15 +8,24 @@ endif
all: gpu-stream-ocl gpu-stream-cuda
gpu-stream-ocl: ocl-stream.cpp
c++ $< $(FLAGS) -o $@ $(LIBS)
gpu-stream-ocl: ocl-stream.cpp common.o Makefile
$(CXX) $(CXXFLAGS) -Wno-deprecated-declarations common.o $< -o $@ $(LDLIBS)
gpu-stream-cuda: cuda-stream.cu
ifeq ($(shell which nvcc > /dev/null; echo $$?), 0)
nvcc $< $(FLAGS) -o $@
else
@echo "Cannot find nvcc, please install CUDA";
common.o: common.cpp Makefile
ifeq ($(shell which nvcc),"")
$(error "Cannot find nvcc, please install CUDA toolkit")
endif
gpu-stream-cuda: cuda-stream.cu common.o Makefile
ifeq ($(shell which nvcc > /dev/null; echo $$?), 0)
nvcc $(CXXFLAGS) common.o $< -o $@
else
$(error "Cannot find nvcc, please install CUDA toolkit")
endif
.PHONY: clean
clean:
rm -f gpu-stream-ocl gpu-stream-cuda
rm -f gpu-stream-ocl gpu-stream-cuda *.o

12906
cl.hpp

File diff suppressed because it is too large Load Diff

View File

@ -4,24 +4,11 @@
#include <vector>
#include <chrono>
#include <cfloat>
#include <iomanip>
#include <cmath>
#include <cuda.h>
#include "common.h"
#define DATATYPE double
unsigned int ARRAY_SIZE = 50000000;
unsigned int NTIMES = 10;
size_t DATATYPE_SIZE = sizeof(double);
bool useFloat = false;
#define MIN(a,b) ((a) < (b)) ? (a) : (b)
#define MAX(a,b) ((a) > (b)) ? (a) : (b)
#define VERSION_STRING "0.0"
void parseArguments(int argc, char *argv[]);
std::string getDeviceName(int device);
struct invaliddevice : public std::exception
@ -54,75 +41,6 @@ void check_cuda_error(void)
}
}
void check_solution(void* a, void* b, void* c)
{
// Generate correct solution
double golda = 1.0;
double goldb = 2.0;
double goldc = 0.0;
float goldaf = 1.0;
float goldbf = 2.0;
float goldcf = 0.0;
const double scalar = 3.0;
const float scalarf = 3.0;
for (unsigned int i = 0; i < NTIMES; i++)
{
// Double
goldc = golda;
goldb = scalar * goldc;
goldc = golda + goldb;
golda = goldb + scalar * goldc;
// Float
goldcf = goldaf;
goldbf = scalarf * goldcf;
goldcf = goldaf + goldbf;
goldaf = goldbf + scalarf * goldcf;
}
// Calculate average error
double erra = 0.0;
double errb = 0.0;
double errc = 0.0;
for (unsigned int i = 0; i < ARRAY_SIZE; i++)
{
if (useFloat)
{
erra += fabsf(((float*)a)[i] - goldaf);
errb += fabsf(((float*)b)[i] - goldbf);
errc += fabsf(((float*)c)[i] - goldcf);
}
else
{
erra += fabs(((double*)a)[i] - (double)golda);
errb += fabs(((double*)b)[i] - (double)goldb);
errc += fabs(((double*)c)[i] - (double)goldc);
}
}
erra /= (double)ARRAY_SIZE;
errb /= (double)ARRAY_SIZE;
errc /= (double)ARRAY_SIZE;
double epsi;
if (useFloat) epsi = 1.0E-6;
else epsi = 1.0E-13;
if (erra > epsi)
std::cout
<< "Validation failed on a[]. Average error " << erra
<< std::endl;
if (errb > epsi)
std::cout
<< "Validation failed on b[]. Average error " << errb
<< std::endl;
if (errc > epsi)
std::cout
<< "Validation failed on c[]. Average error " << errc
<< std::endl;
}
template <typename T>
__global__ void copy(const T * a, T * c)
{
@ -153,8 +71,6 @@ __global__ void triad(T * a, const T * b, const T * c)
a[i] = b[i] + scalar * c[i];
}
int deviceIndex = 0;
int main(int argc, char *argv[])
{
@ -164,8 +80,6 @@ int main(int argc, char *argv[])
<< "Version: " << VERSION_STRING << std::endl
<< "Implementation: CUDA" << std::endl;
try
{
parseArguments(argc, argv);
if (NTIMES < 2) throw badntimes();
@ -188,6 +102,17 @@ int main(int argc, char *argv[])
// Get precision (used to reset later)
std::streamsize ss = std::cout.precision();
size_t DATATYPE_SIZE;
if (useFloat)
{
DATATYPE_SIZE = sizeof(float);
}
else
{
DATATYPE_SIZE = sizeof(double);
}
// Display number of bytes in array
std::cout << std::setprecision(1) << std::fixed
<< "Array size: " << ARRAY_SIZE*DATATYPE_SIZE/1024.0/1024.0 << " MB"
@ -323,7 +248,15 @@ int main(int argc, char *argv[])
check_cuda_error();
cudaMemcpy(h_c, d_c, ARRAY_SIZE*DATATYPE_SIZE, cudaMemcpyDeviceToHost);
check_cuda_error();
check_solution(h_a, h_b, h_c);
if (useFloat)
{
check_solution<float>(h_a, h_b, h_c);
}
else
{
check_solution<double>(h_a, h_b, h_c);
}
// Crunch results
size_t sizes[4] = {
@ -335,16 +268,18 @@ int main(int argc, char *argv[])
double min[4] = {DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX};
double max[4] = {0.0, 0.0, 0.0, 0.0};
double avg[4] = {0.0, 0.0, 0.0, 0.0};
// Ignore first result
for (unsigned int i = 1; i < NTIMES; i++)
{
for (int j = 0; j < 4; j++)
{
avg[j] += timings[i][j];
min[j] = MIN(min[j], timings[i][j]);
max[j] = MAX(max[j], timings[i][j]);
min[j] = std::min(min[j], timings[i][j]);
max[j] = std::max(max[j], timings[i][j]);
}
}
for (int j = 0; j < 4; j++)
avg[j] /= (double)(NTIMES-1);
@ -357,6 +292,7 @@ int main(int argc, char *argv[])
<< std::left << std::setw(12) << "Max"
<< std::left << std::setw(12) << "Average"
<< std::endl;
for (int j = 0; j < 4; j++)
{
std::cout
@ -369,14 +305,6 @@ int main(int argc, char *argv[])
}
}
catch (std::exception& e)
{
std::cerr
<< "Error: "
<< e.what()
<< std::endl;
}
}
std::string getDeviceName(int device)
{
@ -386,26 +314,7 @@ std::string getDeviceName(int device)
return std::string(prop.name);
}
int parseUInt(const char *str, unsigned int *output)
{
char *next;
*output = strtoul(str, &next, 10);
return !strlen(next);
}
int parseInt(const char *str, int *output)
{
char *next;
*output = strtol(str, &next, 10);
return !strlen(next);
}
void parseArguments(int argc, char *argv[])
{
for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "--list"))
void listDevices(void)
{
// Get number of devices
int count;
@ -428,56 +337,5 @@ void parseArguments(int argc, char *argv[])
}
std::cout << std::endl;
}
exit(0);
}
else if (!strcmp(argv[i], "--device"))
{
if (++i >= argc || !parseInt(argv[i], &deviceIndex))
{
std::cout << "Invalid device index" << std::endl;
exit(1);
}
}
else if (!strcmp(argv[i], "--arraysize") || !strcmp(argv[i], "-s"))
{
if (++i >= argc || !parseUInt(argv[i], &ARRAY_SIZE))
{
std::cout << "Invalid array size" << std::endl;
exit(1);
}
}
else if (!strcmp(argv[i], "--numtimes") || !strcmp(argv[i], "-n"))
{
if (++i >= argc || !parseUInt(argv[i], &NTIMES))
{
std::cout << "Invalid number of times" << std::endl;
exit(1);
}
}
else if (!strcmp(argv[i], "--float"))
{
useFloat = true;
DATATYPE_SIZE = sizeof(float);
}
else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
{
std::cout << std::endl;
std::cout << "Usage: ./gpu-stream-cuda [OPTIONS]" << std::endl << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -h --help Print the message" << std::endl;
std::cout << " --list List available devices" << std::endl;
std::cout << " --device INDEX Select device at INDEX" << std::endl;
std::cout << " -s --arraysize SIZE Use SIZE elements in the array" << std::endl;
std::cout << " -n --numtimes NUM Run the test NUM times (NUM >= 2)" << std::endl;
std::cout << " --float Use floats (rather than doubles)" << std::endl;
std::cout << std::endl;
exit(0);
}
else
{
std::cout << "Unrecognized argument '" << argv[i] << "' (try '--help')"
<< std::endl;
exit(1);
}
}
}

View File

@ -4,24 +4,12 @@
#include <vector>
#include <chrono>
#include <cfloat>
#include <iomanip>
#include <cmath>
#define __CL_ENABLE_EXCEPTIONS
#include "cl.hpp"
#include "CL/cl.hpp"
#include "common.h"
unsigned int ARRAY_SIZE = 50000000;
unsigned int NTIMES = 10;
size_t DATATYPE_SIZE = sizeof(double);
bool useFloat = false;
#define MIN(a,b) ((a) < (b)) ? (a) : (b)
#define MAX(a,b) ((a) > (b)) ? (a) : (b)
#define VERSION_STRING "0.0"
void parseArguments(int argc, char *argv[]);
std::string getDeviceName(const cl::Device& device);
unsigned getDeviceList(std::vector<cl::Device>& devices);
@ -50,76 +38,6 @@ struct badntimes : public std::exception
};
void check_solution(void* a, void* b, void* c)
{
// Generate correct solution
double golda = 1.0;
double goldb = 2.0;
double goldc = 0.0;
float goldaf = 1.0;
float goldbf = 2.0;
float goldcf = 0.0;
const double scalar = 3.0;
const float scalarf = 3.0;
for (unsigned int i = 0; i < NTIMES; i++)
{
// Double
goldc = golda;
goldb = scalar * goldc;
goldc = golda + goldb;
golda = goldb + scalar * goldc;
// Float
goldcf = goldaf;
goldbf = scalarf * goldcf;
goldcf = goldaf + goldbf;
goldaf = goldbf + scalarf * goldcf;
}
// Calculate average error
double erra = 0.0;
double errb = 0.0;
double errc = 0.0;
for (unsigned int i = 0; i < ARRAY_SIZE; i++)
{
if (useFloat)
{
erra += fabsf(((float*)a)[i] - goldaf);
errb += fabsf(((float*)b)[i] - goldbf);
errc += fabsf(((float*)c)[i] - goldcf);
}
else
{
erra += fabs(((double*)a)[i] - (double)golda);
errb += fabs(((double*)b)[i] - (double)goldb);
errc += fabs(((double*)c)[i] - (double)goldc);
}
}
erra /= (double)ARRAY_SIZE;
errb /= (double)ARRAY_SIZE;
errc /= (double)ARRAY_SIZE;
double epsi;
if (useFloat) epsi = 1.0E-6;
else epsi = 1.0E-13;
if (erra > epsi)
std::cout
<< "Validation failed on a[]. Average error " << erra
<< std::endl;
if (errb > epsi)
std::cout
<< "Validation failed on b[]. Average error " << errb
<< std::endl;
if (errc > epsi)
std::cout
<< "Validation failed on c[]. Average error " << errc
<< std::endl;
}
cl_uint deviceIndex = 0;
int main(int argc, char *argv[])
{
@ -129,8 +47,6 @@ int main(int argc, char *argv[])
<< "Version: " << VERSION_STRING << std::endl
<< "Implementation: OpenCL" << std::endl;
try
{
parseArguments(argc, argv);
if (NTIMES < 2) throw badntimes();
@ -140,9 +56,30 @@ int main(int argc, char *argv[])
else std::cout << "double";
std::cout << std::endl << std::endl;
if (ARRAY_SIZE % 1024 != 0)
{
unsigned int OLD_ARRAY_SIZE = ARRAY_SIZE;
ARRAY_SIZE -= ARRAY_SIZE % 1024;
std::cout
<< "Warning: array size must divide 1024" << std::endl
<< "Resizing array from " << OLD_ARRAY_SIZE
<< " to " << ARRAY_SIZE << std::endl;
}
// Get precision (used to reset later)
std::streamsize ss = std::cout.precision();
size_t DATATYPE_SIZE;
if (useFloat)
{
DATATYPE_SIZE = sizeof(float);
}
else
{
DATATYPE_SIZE = sizeof(double);
}
// Display number of bytes in array
std::cout << std::setprecision(1) << std::fixed
<< "Array size: " << ARRAY_SIZE*DATATYPE_SIZE/1024.0/1024.0 << " MB"
@ -156,9 +93,13 @@ int main(int argc, char *argv[])
std::cout.precision(ss);
// Open the Kernel source
std::string kernels;
{
std::ifstream in("ocl-stream-kernels.cl");
if (!in.is_open()) throw badfile();
std::string kernels(std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>()));
kernels = std::string (std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>()));
}
// Setup OpenCL
@ -299,7 +240,15 @@ int main(int argc, char *argv[])
queue.enqueueReadBuffer(d_b, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_b);
queue.enqueueReadBuffer(d_c, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_c);
queue.finish();
check_solution(h_a, h_b, h_c);
if (useFloat)
{
check_solution<float>(h_a, h_b, h_c);
}
else
{
check_solution<double>(h_a, h_b, h_c);
}
// Crunch results
size_t sizes[4] = {
@ -311,16 +260,18 @@ int main(int argc, char *argv[])
double min[4] = {DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX};
double max[4] = {0.0, 0.0, 0.0, 0.0};
double avg[4] = {0.0, 0.0, 0.0, 0.0};
// Ignore first result
for (unsigned int i = 1; i < NTIMES; i++)
{
for (int j = 0; j < 4; j++)
{
avg[j] += timings[i][j];
min[j] = MIN(min[j], timings[i][j]);
max[j] = MAX(max[j], timings[i][j]);
min[j] = std::min(min[j], timings[i][j]);
max[j] = std::max(max[j], timings[i][j]);
}
}
for (int j = 0; j < 4; j++)
avg[j] /= (double)(NTIMES-1);
@ -333,6 +284,7 @@ int main(int argc, char *argv[])
<< std::left << std::setw(12) << "Max"
<< std::left << std::setw(12) << "Average"
<< std::endl;
for (int j = 0; j < 4; j++)
{
std::cout
@ -343,24 +295,6 @@ int main(int argc, char *argv[])
<< std::left << std::setw(12) << std::setprecision(5) << avg[j]
<< std::endl;
}
}
// Catch OpenCL Errors and display information
catch (cl::Error& e)
{
std::cerr
<< "Error: "
<< e.what()
<< "(" << e.err() << ")"
<< std::endl;
}
catch (std::exception& e)
{
std::cerr
<< "Error: "
<< e.what()
<< std::endl;
}
}
unsigned getDeviceList(std::vector<cl::Device>& devices)
@ -396,19 +330,7 @@ std::string getDeviceName(const cl::Device& device)
return name;
}
int parseUInt(const char *str, cl_uint *output)
{
char *next;
*output = strtoul(str, &next, 10);
return !strlen(next);
}
void parseArguments(int argc, char *argv[])
{
for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "--list"))
void listDevices(void)
{
// Get list of devices
std::vector<cl::Device> devices;
@ -429,56 +351,5 @@ void parseArguments(int argc, char *argv[])
}
std::cout << std::endl;
}
exit(0);
}
else if (!strcmp(argv[i], "--device"))
{
if (++i >= argc || !parseUInt(argv[i], &deviceIndex))
{
std::cout << "Invalid device index" << std::endl;
exit(1);
}
}
else if (!strcmp(argv[i], "--arraysize") || !strcmp(argv[i], "-s"))
{
if (++i >= argc || !parseUInt(argv[i], &ARRAY_SIZE))
{
std::cout << "Invalid array size" << std::endl;
exit(1);
}
}
else if (!strcmp(argv[i], "--numtimes") || !strcmp(argv[i], "-n"))
{
if (++i >= argc || !parseUInt(argv[i], &NTIMES))
{
std::cout << "Invalid number of times" << std::endl;
exit(1);
}
}
else if (!strcmp(argv[i], "--float"))
{
useFloat = true;
DATATYPE_SIZE = sizeof(float);
}
else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
{
std::cout << std::endl;
std::cout << "Usage: ./gpu-stream-ocl [OPTIONS]" << std::endl << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -h --help Print the message" << std::endl;
std::cout << " --list List available devices" << std::endl;
std::cout << " --device INDEX Select device at INDEX" << std::endl;
std::cout << " -s --arraysize SIZE Use SIZE elements in the array" << std::endl;
std::cout << " -n --numtimes NUM Run the test NUM times (NUM >= 2)" << std::endl;
std::cout << " --float Use floats (rather than doubles)" << std::endl;
std::cout << std::endl;
exit(0);
}
else
{
std::cout << "Unrecognized argument '" << argv[i] << "' (try '--help')"
<< std::endl;
exit(1);
}
}
}