boinc/samples/atiopencl/atiopencl.hpp

230 lines
6.1 KiB
C++
Raw Normal View History

// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
//
// See http://boinc.berkeley.edu/trac/wiki/GPUApp for any compiling issues.
// Contributor: Tuan Le (tuanle86@berkeley.edu)
2010-06-15 18:09:28 +00:00
#ifndef ATIOPENCL_H_
#define ATIOPENCL_H_
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
2010-06-25 21:10:02 +00:00
#include <CL/opencl.h>
#endif
#include <string.h>
2010-06-15 18:09:28 +00:00
#include <math.h>
#include <time.h>
#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 "../../atiopencl_kernels.cl" // for Linux and Mac
2010-06-15 18:09:28 +00:00
#define CHECKPOINT_FILE "matrix_inversion_state"
#define LOCAL_WORK_SIZE 1
#define GLOBAL_WORK_SIZE 400
#define MATRIX_SIZE 10
#define NUM_ITERATIONS 501 // execute the kernel NUM_ITERATIONS times
2010-06-15 18:09:28 +00:00
#ifdef _WIN32
#include "boinc_win.h"
#else
#ifndef __APPLE__
2010-06-15 18:09:28 +00:00
#include "config.h"
#endif
2010-06-15 18:09:28 +00:00
#include <cstdio>
#include <cctype>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <csignal>
#include <unistd.h>
#endif
#include "str_util.h"
#include "util.h"
#include "filesys.h"
#include "boinc_api.h"
#include "mfile.h"
#include "graphics2.h"
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
/*** 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;
size_t globalThreads[1]; // 1D var for Total # of work items
size_t localThreads[1]; // 1D var for # of work items in the work group
/*
* Input data is stored here.
*/
2010-06-15 18:09:28 +00:00
cl_float *input;
/*
* Output data is stored here.
*/
2010-06-15 18:09:28 +00:00
cl_float *output;
2010-06-15 18:09:28 +00:00
/* problem size for a 2D matrix. */
// Note: we will handle the problem as a 1D matrix.
cl_uint width;
2010-06-15 18:09:28 +00:00
cl_uint height;
/* The memory buffer that is used as input/output for OpenCL kernel */
cl_mem inputBuffer; //in this sample app, we will read the result
//from the device back to host from inputBuffer as well.
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;
/*** FUNCTION DECLARATIONS ***/
2010-06-15 18:09:28 +00:00
/*
* Create an input file filled with random data of type cl_float.
*/
void generate_random_input_file(int n);
2010-06-15 18:09:28 +00:00
/*
* 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 get_matrix_size(FILE *infile);
2010-06-15 18:09:28 +00:00
/*
* Read the float values from input file into "input" array.
*/
void fetch_elements_into_host_memory(FILE *infile, cl_float *input);
2010-06-15 18:09:28 +00:00
/*
* BOINC functions
*/
/* Do a billion floating-point ops */
2010-06-15 18:09:28 +00:00
static double do_a_giga_flop(int foo);
/* Save the computation state into checkpoint file */
2010-06-15 18:09:28 +00:00
int do_checkpoint(MFILE& mf, int n, cl_float *input, int matrixSize);
#ifdef APP_GRAPHICS
void update_shmem() {
if (!shmem) return;
// always do this; otherwise a graphics app will immediately
// assume we're not alive
shmem->update_time = dtime();
// Check whether a graphics app is running,
// and don't bother updating shmem if so.
// This doesn't matter here,
// but may be worth doing if updating shmem is expensive.
//
if (shmem->countdown > 0) {
// the graphics app sets this to 5 every time it renders a frame
shmem->countdown--;
} else {
return;
}
shmem->fraction_done = boinc_get_fraction_done();
shmem->cpu_time = boinc_worker_thread_cpu_time();;
boinc_get_status(&shmem->status);
}
#endif
2010-06-15 18:09:28 +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 initialize_cl(void);
int initialize_host(FILE *infile);
2010-06-15 18:09:28 +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.
*/
char *convert_to_string(const char * filename);
/*
* 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 run_cl_kernels(void);
/* Releases OpenCL resources (Context, Memory etc.) */
int cleanup_cl(void);
/* Releases program's resources */
void cleanup_host(void);
2010-06-15 18:09:28 +00:00
/* Write the result to output file */
void print_to_file(MFILE *out, float *h_odata, int n);
2010-06-15 18:09:28 +00:00
/*
* Functions used to inverst matrix. Call kernels inside.
*/
2010-06-15 18:09:28 +00:00
void invert(cl_float * input,
cl_float *output,
int n);
2010-06-15 18:09:28 +00:00
void invertge(cl_float * AI_d,
int lda,
int n);
#endif /* #ifndef ATIOPENCL_H_ */