// This file is part of BOINC. // http://boinc.berkeley.edu // Copyright (C) 2013 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 . // // This program serves as both // - An example BOINC-CUDA application, illustrating the use of the BOINC API // and CUDA API. [ SEE NOTE BELOW ] // - A program for testing various features of BOINC. // // The program reads the input nxn matrix from the "input" file, inverts the // matrix NUM_ITERATIONS times and write to "output" file. // // command line options // -run_slow: sleep 1 second after each character // -cpu_time N: use about N CPU seconds after copying files // -early_exit: exit(10) after 30 chars // -early_crash: crash after 30 chars // // See https://github.com/BOINC/boinc/wiki/GPUApp // Contributor: Tuan Le (tuanle86@berkeley.edu) // // NOTE: As currently written, this sample is of limited usefulness, as it // is missing two important features: // * Code to determine the correct device assigned by BOINC. It needs to get // the device number from the gpu_opencl_dev_index field of init_data.xml // if it exists, else from the gpu_device_num field of init_data.xml if that // exists, else from the --device or -device argument passed by the client. // See api/boinc_opencl.cpp for code which does this. // * Code to select which NVIDIA GPU to use if there are more than one on the // system; it needs to call cudaSetDevice(). // #include "cuda.h" #include "cuda_config.h" using std::string; /*** GLOBALS ***/ bool run_slow = false; bool early_exit = false; bool early_crash = false; double cpu_time = 20, comp_result; int main(int argc, char** argv) { int i, retval, lastInversion=0, checkpointExists=0, dimension=0; double fd; char input_path[512], output_path[512], chkpt_path[512], buf[256]; REAL* h_idata; MFILE out; FILE* state, *infile; double num=0; generate_random_input_file(MATRIX_SIZE); //call this if you don't want to //construct the input file manually for (i=0; i30) { exit(-10); } if (early_crash && i>30) { boinc_crash(); } if (boinc_time_to_checkpoint()) { //if (i==7) { printf("Perform checkpointing at inversion # %d\n",i); //we'll need to write the current matrix to the state file. retval = do_checkpoint(out, i, h_idata, dimension); if (retval) { fprintf(stderr, "%s APP: matrix_inversion checkpoint failed %d\n", boinc_msg_prefix(buf, sizeof(buf)), retval ); exit(retval); } boinc_checkpoint_completed(); } fd = i/NUM_ITERATIONS; if (cpu_time) fd /= 2; boinc_fraction_done(fd); } out.printf("\n\n----------------- Final inversion #%d----------------\n\n", NUM_ITERATIONS); print_to_file(&out,h_idata,dimension); cudaFreeHost( h_idata ); retval = out.flush(); //force the output file to be closed. if (retval) { fprintf(stderr, "%s APP: matrix_inversion flush failed %d\n", boinc_msg_prefix(buf, sizeof(buf)), retval ); exit(1); } // burn up some CPU time if needed // if (cpu_time) { printf("\nBurning up some CPU time ... \n"); double start = dtime(); for (i=0; ; i++) { double e = dtime()-start; if (e > cpu_time) break; fd = .5 + .5*(e/cpu_time); boinc_fraction_done(fd); if (boinc_time_to_checkpoint()) { retval = do_checkpoint(out, NUM_ITERATIONS, h_idata, dimension); if (retval) { fprintf(stderr, "%s APP: maxtrix_inversion checkpoint failed %d\n", boinc_msg_prefix(buf, sizeof(buf)), retval ); exit(1); } boinc_checkpoint_completed(); } comp_result = do_a_giga_flop(i); } } boinc_fraction_done(1); #ifdef APP_GRAPHICS update_shmem(); #endif printf("\nDone! Please press ENTER to exit. "); getchar(); boinc_finish(0); } /*** BOINC FUNCTION DEFINITIONS ***/ /* Do a billion floating-point ops */ static double do_a_giga_flop(int foo) { double x = 3.14159*foo; int i; for (i=0; i<500000000; i++) { x += 5.12313123; x *= 0.5398394834; } return x; } /* Save the computation state into checkpoint file */ int do_checkpoint(MFILE& mf, int n, REAL *h_idata, int dimension) { int retval; string resolved_name; FILE* f = fopen("temp", "w"); if (!f) { return 1; } fprintf(f, "%d", n); //write inversion number fprintf(f, " "); fprintf(f, "%d", dimension); //write dimension fprintf(f, " "); for (int i=0;i height, the matrix is * non-invertible. */ int get_matrix_dimension(FILE *infile) { int w=0; char c; fseek(infile,0,SEEK_SET); while (true) { do { c=fgetc(infile); if (c == EOF || c == '\n') { goto exitLoop; } } while (isspace(c)); if (isdigit(c) || c=='.' || c=='-') { ++w; } do { c=fgetc(infile); if (c == EOF || c == '\n') { goto exitLoop; } } while (isdigit(c) || c=='.' || c=='-'); if (c==EOF || c == '\n') { break; } } exitLoop: return w; } /* Read the REAL values from input file into host array. */ void fetch_elements_into_host_memory(FILE *infile, REAL *h_idata) { float num=0; int i=0; fseek(infile,0,SEEK_SET); while (fscanf(infile,"%f",&num)==1) { h_idata[i]=num; ++i; } } /* Write the result to output file */ void print_to_file(MFILE *out, float *h_odata, int dimension) { int count=0; int move=0; int num_elements=dimension*dimension; while (num_elements>0) { out->printf("%15f ",h_odata[move]); ++count; ++move; if (count==dimension) { out->printf("\n"); count=0; } --num_elements; } }