~ubuntu-branches/ubuntu/precise/boinc/precise

« back to all changes in this revision

Viewing changes to samples/nvopencl/nvopencl.hpp

Tags: 6.12.8+dfsg-1
* New upstream release.
* Simplified debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// This file is part of BOINC.
 
2
// http://boinc.berkeley.edu
 
3
// Copyright (C) 2008 University of California
 
4
//
 
5
// BOINC is free software; you can redistribute it and/or modify it
 
6
// under the terms of the GNU Lesser General Public License
 
7
// as published by the Free Software Foundation,
 
8
// either version 3 of the License, or (at your option) any later version.
 
9
//
 
10
// BOINC is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
13
// See the GNU Lesser General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU Lesser General Public License
 
16
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
 
17
//
 
18
// See http://boinc.berkeley.edu/trac/wiki/GPUApp for any compiling issues.
 
19
// Contributor: Tuan Le (tuanle86@berkeley.edu)
 
20
 
 
21
#ifndef NVOPENCL_H_
 
22
#define NVOPENCL_H_
 
23
 
 
24
#include <CL/cl.h>
 
25
#include <oclUtils.h>
 
26
#include <string.h>
 
27
#include <math.h>
 
28
#include <time.h>
 
29
#include <cstdlib>
 
30
#include <iostream>
 
31
#include <string>
 
32
#include <fstream>
 
33
 
 
34
#define INPUT_FILENAME "input"
 
35
#define OUTPUT_FILENAME "output"
 
36
#define KERNELS_FILENAME "nvopencl_kernels.cl"
 
37
#define KERNELS_FILEPATH "../../nvopencl_kernels.cl" // for Linux and Mac
 
38
#define CHECKPOINT_FILE "matrix_inversion_state"
 
39
 
 
40
#define LOCAL_WORK_SIZE 1
 
41
#define GLOBAL_WORK_SIZE 400
 
42
#define MATRIX_SIZE 10
 
43
#define NUM_ITERATIONS 501 // execute the kernel NUM_ITERATIONS times
 
44
 
 
45
#ifdef _WIN32
 
46
#include "boinc_win.h"
 
47
#else
 
48
#include "config.h"
 
49
#include <cstdio>
 
50
#include <cctype>
 
51
#include <ctime>
 
52
#include <cstring>
 
53
#include <cstdlib>
 
54
#include <csignal>
 
55
#include <unistd.h>
 
56
#endif
 
57
 
 
58
#include "str_util.h"
 
59
#include "util.h"
 
60
#include "filesys.h"
 
61
#include "boinc_api.h"
 
62
#include "mfile.h"
 
63
#include "graphics2.h"
 
64
 
 
65
struct UC_SHMEM {
 
66
    double update_time;
 
67
    double fraction_done;
 
68
    double cpu_time;
 
69
    BOINC_STATUS status;
 
70
    int countdown;
 
71
    // graphics app sets this to 5 repeatedly,
 
72
    // main program decrements it once/sec.
 
73
    // If it's zero, don't bother updating shmem
 
74
};
 
75
 
 
76
#ifdef APP_GRAPHICS
 
77
UC_SHMEM* shmem;
 
78
#endif
 
79
 
 
80
/*** GLOBALS ***/
 
81
 
 
82
bool run_slow = false;
 
83
bool early_exit = false;
 
84
bool early_crash = false;
 
85
bool early_sleep = false;
 
86
double cpu_time = 20, comp_result;
 
87
bool isStateFileInUse = false;
 
88
const char *source;
 
89
 
 
90
size_t globalThreads[1]; // 1D var for Total # of work items
 
91
size_t localThreads[1];  // 1D var for # of work items in the work group        
 
92
 
 
93
/*
 
94
 * Input data is stored here.
 
95
 */
 
96
cl_float *input;
 
97
 
 
98
/*
 
99
 * Output data is stored here.
 
100
 */
 
101
cl_float *output;
 
102
 
 
103
/* problem size for a 2D matrix. */
 
104
// Note: we will handle the problem as a 1D matrix.
 
105
cl_uint width;
 
106
cl_uint height;
 
107
 
 
108
/* The memory buffer that is used as input/output for OpenCL kernel */
 
109
cl_mem   inputBuffer; //in this sample app, we will read the result
 
110
                      //from the device back to host from inputBuffer as well.
 
111
cl_context          context;
 
112
cl_device_id        *devices;
 
113
cl_command_queue    commandQueue;
 
114
 
 
115
cl_program program;
 
116
 
 
117
/* This program uses three kernels */
 
118
cl_kernel  GEStep1A_kernel;
 
119
cl_kernel  GEStep2_kernel;
 
120
cl_kernel  GEStep3_kernel;
 
121
 
 
122
/*** FUNCTION DECLARATIONS ***/
 
123
 
 
124
/*
 
125
 * Create an input file filled with random data of type cl_float.
 
126
 */
 
127
void generate_random_input_file(int n);
 
128
 
 
129
/*
 
130
 * Parse the input file and determine the size of the matrix.
 
131
 * This is an nxn matrix. Note: if width<> height, the matrix is
 
132
 * non-invertible.
 
133
 */
 
134
int get_matrix_size(FILE *infile);
 
135
 
 
136
/*
 
137
 * Read the float values from input file into "input" array.
 
138
 */
 
139
void fetch_elements_into_host_memory(FILE *infile, cl_float *input);
 
140
 
 
141
/*
 
142
 * BOINC functions
 
143
 */
 
144
 
 
145
/* Do a billion floating-point ops */
 
146
static double do_a_giga_flop(int foo);
 
147
 
 
148
/* Save the computation state into checkpoint file */
 
149
int do_checkpoint(MFILE& mf, int n, cl_float *input, int matrixSize);
 
150
 
 
151
#ifdef APP_GRAPHICS
 
152
void update_shmem() {
 
153
    if (!shmem) return;
 
154
 
 
155
    // always do this; otherwise a graphics app will immediately
 
156
    // assume we're not alive
 
157
    shmem->update_time = dtime();
 
158
 
 
159
    // Check whether a graphics app is running,
 
160
    // and don't bother updating shmem if so.
 
161
    // This doesn't matter here,
 
162
    // but may be worth doing if updating shmem is expensive.
 
163
    //
 
164
    if (shmem->countdown > 0) {
 
165
        // the graphics app sets this to 5 every time it renders a frame
 
166
        shmem->countdown--;
 
167
    } else {
 
168
        return;
 
169
    }
 
170
    shmem->fraction_done = boinc_get_fraction_done();
 
171
    shmem->cpu_time = boinc_worker_thread_cpu_time();;
 
172
    boinc_get_status(&shmem->status);
 
173
}
 
174
#endif
 
175
 
 
176
/*
 
177
 * OpenCL related initialisations are done here.
 
178
 * Context, Device list, Command Queue are set up.
 
179
 * Calls are made to set up OpenCL memory buffers that this program uses
 
180
 * and to load the programs into memory and get kernel handles.
 
181
 */
 
182
int initialize_cl(void);
 
183
 
 
184
int initialize_host(FILE *infile);
 
185
 
 
186
/*
 
187
 * Read the file which contains kernel definitions, and stores the file content
 
188
 * into a char array which is used as an argument to clCreateProgramWithSource.
 
189
 */
 
190
char *convert_to_string(const char * filename);
 
191
 
 
192
/*
 
193
 * This is called once the OpenCL context, memory etc. are set up,
 
194
 * the program is loaded into memory and the kernel handles are ready.
 
195
 * 
 
196
 * It sets the values for kernels' arguments and enqueues calls to the kernels
 
197
 * on to the command queue and waits till the calls have finished execution.
 
198
 *
 
199
 * It also gets kernel start and end time if profiling is enabled.
 
200
 */
 
201
int run_cl_kernels(void);
 
202
 
 
203
/* Releases OpenCL resources (Context, Memory etc.) */
 
204
int cleanup_cl(void);
 
205
 
 
206
/* Releases program's resources */
 
207
void cleanup_host(void);
 
208
 
 
209
/* Write the result to output file */
 
210
void print_to_file(MFILE *out, float *h_odata, int n);
 
211
 
 
212
/*
 
213
 *      Functions used to inverst matrix. Call kernels inside.
 
214
 */
 
215
void invert(cl_float * input,
 
216
            cl_float *output,
 
217
            int n);
 
218
 
 
219
void invertge(cl_float * AI_d,
 
220
              int lda,
 
221
              int n);
 
222
 
 
223
#endif  /* #ifndef NVOPENCL_H_ */
 
 
b'\\ No newline at end of file'