2010-07-15 19:29:43 +00:00
|
|
|
// 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/>.
|
|
|
|
//
|
2010-07-26 21:16:36 +00:00
|
|
|
// See http://boinc.berkeley.edu/trac/wiki/GPUApp for any compiling issues.
|
2010-07-15 19:29:43 +00:00
|
|
|
// Contributor: Tuan Le (tuanle86@berkeley.edu)
|
2010-06-09 22:18:37 +00:00
|
|
|
|
2010-06-24 23:53:31 +00:00
|
|
|
#ifndef NVOPENCL_H_
|
|
|
|
#define NVOPENCL_H_
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
#include <CL/cl.h>
|
2010-07-26 21:16:36 +00:00
|
|
|
#include <oclUtils.h>
|
2010-06-09 22:18:37 +00:00
|
|
|
#include <string.h>
|
2010-06-24 23:53:31 +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-24 23:53:31 +00:00
|
|
|
#define INPUT_FILENAME "input"
|
|
|
|
#define OUTPUT_FILENAME "output"
|
|
|
|
#define KERNELS_FILENAME "nvopencl_kernels.cl"
|
2010-07-15 22:17:41 +00:00
|
|
|
#define KERNELS_FILEPATH "../../nvopencl_kernels.cl" // for Linux and Mac
|
2010-06-24 23:53:31 +00:00
|
|
|
#define CHECKPOINT_FILE "matrix_inversion_state"
|
2010-07-26 21:16:36 +00:00
|
|
|
|
2010-07-26 21:54:32 +00:00
|
|
|
#define LOCAL_WORK_SIZE 1
|
|
|
|
#define GLOBAL_WORK_SIZE 400
|
2010-07-15 19:29:43 +00:00
|
|
|
#define MATRIX_SIZE 10
|
|
|
|
#define NUM_ITERATIONS 501 // execute the kernel NUM_ITERATIONS times
|
2010-06-24 23:53:31 +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 "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;
|
2010-07-15 19:29:43 +00:00
|
|
|
// graphics app sets this to 5 repeatedly,
|
|
|
|
// main program decrements it once/sec.
|
|
|
|
// If it's zero, don't bother updating shmem
|
2010-06-24 23:53:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
UC_SHMEM* shmem;
|
|
|
|
#endif
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
/*** GLOBALS ***/
|
|
|
|
|
2010-06-24 23:53:31 +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-07-26 21:16:36 +00:00
|
|
|
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
|
|
|
|
|
2010-06-09 22:18:37 +00:00
|
|
|
/*
|
|
|
|
* Input data is stored here.
|
|
|
|
*/
|
2010-06-24 23:53:31 +00:00
|
|
|
cl_float *input;
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Output data is stored here.
|
|
|
|
*/
|
2010-06-24 23:53:31 +00:00
|
|
|
cl_float *output;
|
2010-06-09 22:18:37 +00:00
|
|
|
|
2010-06-24 23:53:31 +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-24 23:53:31 +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-07-15 19:29:43 +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-24 23:53:31 +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-24 23:53:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an input file filled with random data of type cl_float.
|
|
|
|
*/
|
2010-07-15 19:29:43 +00:00
|
|
|
void generate_random_input_file(int n);
|
2010-06-24 23:53:31 +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.
|
|
|
|
*/
|
2010-07-15 19:29:43 +00:00
|
|
|
int get_matrix_size(FILE *infile);
|
2010-06-24 23:53:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the float values from input file into "input" array.
|
|
|
|
*/
|
2010-07-15 19:29:43 +00:00
|
|
|
void fetch_elements_into_host_memory(FILE *infile, cl_float *input);
|
2010-06-24 23:53:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* BOINC functions
|
|
|
|
*/
|
2010-07-15 19:29:43 +00:00
|
|
|
|
|
|
|
/* Do a billion floating-point ops */
|
2010-06-24 23:53:31 +00:00
|
|
|
static double do_a_giga_flop(int foo);
|
2010-07-15 19:29:43 +00:00
|
|
|
|
|
|
|
/* Save the computation state into checkpoint file */
|
2010-06-24 23:53:31 +00:00
|
|
|
int do_checkpoint(MFILE& mf, int n, cl_float *input, int matrixSize);
|
2010-07-15 19:29:43 +00:00
|
|
|
|
|
|
|
#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-24 23:53:31 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2010-07-15 19:29:43 +00:00
|
|
|
int initialize_cl(void);
|
2010-06-09 22:18:37 +00:00
|
|
|
|
2010-07-15 19:29:43 +00:00
|
|
|
int initialize_host(FILE *infile);
|
2010-06-24 23:53:31 +00:00
|
|
|
|
2010-06-09 22:18:37 +00:00
|
|
|
/*
|
2010-06-24 23:53:31 +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-07-15 19:29:43 +00:00
|
|
|
char *convert_to_string(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.
|
|
|
|
*/
|
2010-07-15 19:29:43 +00:00
|
|
|
int run_cl_kernels(void);
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
/* Releases OpenCL resources (Context, Memory etc.) */
|
2010-07-15 19:29:43 +00:00
|
|
|
int cleanup_cl(void);
|
2010-06-09 22:18:37 +00:00
|
|
|
|
|
|
|
/* Releases program's resources */
|
2010-07-15 19:29:43 +00:00
|
|
|
void cleanup_host(void);
|
2010-06-09 22:18:37 +00:00
|
|
|
|
2010-06-24 23:53:31 +00:00
|
|
|
/* Write the result to output file */
|
2010-07-15 19:29:43 +00:00
|
|
|
void print_to_file(MFILE *out, float *h_odata, int n);
|
2010-06-24 23:53:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions used to inverst matrix. Call kernels inside.
|
2010-06-09 22:18:37 +00:00
|
|
|
*/
|
2010-06-24 23:53:31 +00:00
|
|
|
void invert(cl_float * input,
|
2010-07-15 19:29:43 +00:00
|
|
|
cl_float *output,
|
|
|
|
int n);
|
2010-06-09 22:18:37 +00:00
|
|
|
|
2010-06-24 23:53:31 +00:00
|
|
|
void invertge(cl_float * AI_d,
|
2010-07-15 19:29:43 +00:00
|
|
|
int lda,
|
|
|
|
int n);
|
2010-06-09 22:18:37 +00:00
|
|
|
|
2010-07-15 19:29:43 +00:00
|
|
|
#endif /* #ifndef NVOPENCL_H_ */
|