2010-06-15 18:09:28 +00:00
|
|
|
/*
|
|
|
|
* atiopencl.hpp
|
|
|
|
* Author: Tuan Le
|
|
|
|
* Date: 06/14/2010
|
|
|
|
* University of California, Berkeley
|
2010-06-22 20:21:41 +00:00
|
|
|
* Berkeley Space Sciences Lab
|
2010-06-15 18:09:28 +00:00
|
|
|
* tuanle86@berkeley.edu
|
|
|
|
*/
|
2010-06-09 22:18:37 +00:00
|
|
|
|
2010-06-15 18:09:28 +00:00
|
|
|
#ifndef ATIOPENCL_H_
|
|
|
|
#define ATIOPENCL_H_
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
#include <CL/cl.h>
|
|
|
|
#include <string.h>
|
2010-06-15 18:09:28 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
2010-06-09 22:18:37 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
|
2010-06-15 18:09:28 +00:00
|
|
|
#define INPUT_FILENAME "input"
|
|
|
|
#define OUTPUT_FILENAME "output"
|
|
|
|
#define KERNELS_FILENAME "atiopencl_kernels.cl"
|
|
|
|
#define KERNELS_FILEPATH "../samples/atiopencl/atiopencl_kernels.cl"
|
|
|
|
#define CHECKPOINT_FILE "matrix_inversion_state"
|
|
|
|
#define MATRIX_SIZE 20
|
2010-06-22 20:21:41 +00:00
|
|
|
#define NUM_ITERATIONS 51 // execute the kernel NUM_ITERATIONS times
|
2010-06-15 18:09:28 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "boinc_win.h"
|
|
|
|
#else
|
|
|
|
#include "config.h"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cctype>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <csignal>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//#include <cuda_runtime.h>
|
|
|
|
//#include <cublas.h>
|
|
|
|
|
|
|
|
#include "str_util.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
#include "boinc_api.h"
|
|
|
|
#include "mfile.h"
|
|
|
|
#include "graphics2.h"
|
|
|
|
|
2010-06-22 20:21:41 +00:00
|
|
|
struct UC_SHMEM {
|
|
|
|
double update_time;
|
|
|
|
double fraction_done;
|
|
|
|
double cpu_time;
|
|
|
|
BOINC_STATUS status;
|
|
|
|
int countdown;
|
|
|
|
// graphics app sets this to 5 repeatedly,
|
|
|
|
// main program decrements it once/sec.
|
|
|
|
// If it's zero, don't bother updating shmem
|
|
|
|
};
|
|
|
|
|
2010-06-15 18:09:28 +00:00
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
UC_SHMEM* shmem;
|
|
|
|
#endif
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
/*** GLOBALS ***/
|
|
|
|
|
2010-06-15 18:09:28 +00:00
|
|
|
bool run_slow = false;
|
|
|
|
bool early_exit = false;
|
|
|
|
bool early_crash = false;
|
|
|
|
bool early_sleep = false;
|
|
|
|
double cpu_time = 20, comp_result;
|
|
|
|
bool isStateFileInUse = false;
|
|
|
|
const char *source;
|
|
|
|
|
2010-06-09 22:18:37 +00:00
|
|
|
/*
|
|
|
|
* Input data is stored here.
|
|
|
|
*/
|
2010-06-15 18:09:28 +00:00
|
|
|
cl_float *input;
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Output data is stored here.
|
|
|
|
*/
|
2010-06-15 18:09:28 +00:00
|
|
|
cl_float *output;
|
2010-06-09 22:18:37 +00:00
|
|
|
|
2010-06-15 18:09:28 +00:00
|
|
|
/* problem size for a 2D matrix. */
|
|
|
|
// Note: we will handle the problem as a 1D matrix.
|
2010-06-09 22:18:37 +00:00
|
|
|
cl_uint width;
|
2010-06-15 18:09:28 +00:00
|
|
|
cl_uint height;
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
/* The memory buffer that is used as input/output for OpenCL kernel */
|
2010-06-15 18:09:28 +00:00
|
|
|
cl_mem inputBuffer; //in this sample app, we will read the result from the device back to host from inputBuffer as well.
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
cl_context context;
|
|
|
|
cl_device_id *devices;
|
|
|
|
cl_command_queue commandQueue;
|
|
|
|
|
|
|
|
cl_program program;
|
|
|
|
|
2010-06-15 18:09:28 +00:00
|
|
|
/* This program uses three kernels */
|
|
|
|
cl_kernel GEStep1A_kernel;
|
|
|
|
cl_kernel GEStep2_kernel;
|
|
|
|
cl_kernel GEStep3_kernel;
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
/*** FUNCTION DECLARATIONS ***/
|
2010-06-15 18:09:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an input file filled with random data of type cl_float.
|
|
|
|
*/
|
|
|
|
void generateRandomInputFile(int n);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the input file and determine the size of the matrix.
|
|
|
|
* This is an nxn matrix. Note: if width<> height, the matrix is
|
|
|
|
* non-invertible.
|
|
|
|
*/
|
|
|
|
int getMatrixSize(FILE *infile);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the float values from input file into "input" array.
|
|
|
|
*/
|
|
|
|
void fetchElementsIntoHostMemory(FILE *infile, cl_float *input);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BOINC functions
|
|
|
|
*/
|
|
|
|
static double do_a_giga_flop(int foo);
|
|
|
|
int do_checkpoint(MFILE& mf, int n, cl_float *input, int matrixSize);
|
|
|
|
void update_shmem();
|
|
|
|
|
2010-06-09 22:18:37 +00:00
|
|
|
/*
|
|
|
|
* OpenCL related initialisations are done here.
|
|
|
|
* Context, Device list, Command Queue are set up.
|
|
|
|
* Calls are made to set up OpenCL memory buffers that this program uses
|
|
|
|
* and to load the programs into memory and get kernel handles.
|
|
|
|
*/
|
|
|
|
int initializeCL(void);
|
|
|
|
|
2010-06-15 18:09:28 +00:00
|
|
|
int initializeHost(FILE *infile);
|
|
|
|
|
2010-06-09 22:18:37 +00:00
|
|
|
/*
|
2010-06-15 18:09:28 +00:00
|
|
|
* Read the file which contains kernel definitions, and stores the file content
|
|
|
|
* into a char array which is used as an argument to clCreateProgramWithSource.
|
2010-06-09 22:18:37 +00:00
|
|
|
*/
|
2010-06-15 18:09:28 +00:00
|
|
|
char *convertToString(const char * filename);
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called once the OpenCL context, memory etc. are set up,
|
|
|
|
* the program is loaded into memory and the kernel handles are ready.
|
|
|
|
*
|
|
|
|
* It sets the values for kernels' arguments and enqueues calls to the kernels
|
|
|
|
* on to the command queue and waits till the calls have finished execution.
|
|
|
|
*
|
|
|
|
* It also gets kernel start and end time if profiling is enabled.
|
|
|
|
*/
|
|
|
|
int runCLKernels(void);
|
|
|
|
|
|
|
|
/* Releases OpenCL resources (Context, Memory etc.) */
|
|
|
|
int cleanupCL(void);
|
|
|
|
|
|
|
|
/* Releases program's resources */
|
|
|
|
void cleanupHost(void);
|
|
|
|
|
2010-06-15 18:09:28 +00:00
|
|
|
/* Write the result to output file */
|
|
|
|
void printToFile(MFILE *out, float *h_odata, int n);
|
|
|
|
|
2010-06-09 22:18:37 +00:00
|
|
|
/*
|
2010-06-15 18:09:28 +00:00
|
|
|
* Check if the device is able to support the requested number of work items.
|
|
|
|
*/
|
|
|
|
int checkDeviceCapability(size_t *globalThreads,
|
|
|
|
size_t *localThreads);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions used to inverst matrix. Call kernels inside.
|
2010-06-09 22:18:37 +00:00
|
|
|
*/
|
2010-06-15 18:09:28 +00:00
|
|
|
void invert(cl_float * input,
|
|
|
|
cl_float *output,
|
|
|
|
int n);
|
2010-06-09 22:18:37 +00:00
|
|
|
|
2010-06-15 18:09:28 +00:00
|
|
|
void invertge(cl_float * AI_d,
|
|
|
|
int lda,
|
|
|
|
int n);
|
2010-06-09 22:18:37 +00:00
|
|
|
|
2010-06-22 20:21:41 +00:00
|
|
|
#endif /* #ifndef ATIOPENCL_H_ */
|