// 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 . // // This program serves as both // - An example BOINC-CUDA application, illustrating the use of the BOINC API // and CUDA API. // - 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 http://boinc.berkeley.edu/trac/wiki/GPUApp for any compiling issues // Contributor: Tuan Le (tuanle86@berkeley.edu) #include "cuda.h" #include "cuda_config.h" using std::string; /*** GLOBALS ***/ bool run_slow = false; bool early_exit = false; bool early_crash = false; bool early_sleep = 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; 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 (early_sleep && i>30) { g_sleep = true; while (1) boinc_sleep(1); } 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 (int 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); } #ifdef _WIN32 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) { LPSTR command_line; char* argv[100]; int argc; command_line = GetCommandLine(); argc = parse_command_line( command_line, argv ); return main(argc, argv); } #endif /*** 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; } }