2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2007-07-20 18:46:43 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2007-07-20 18:46:43 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// 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.
|
2007-07-20 18:46:43 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2007-07-20 18:46:43 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2007-07-20 18:46:43 +00:00
|
|
|
|
2008-01-29 19:55:25 +00:00
|
|
|
// This program serves as both
|
|
|
|
// - An example BOINC application, illustrating the use of the BOINC API
|
|
|
|
// - A program for testing various features of BOINC
|
|
|
|
//
|
2007-07-20 18:46:43 +00:00
|
|
|
// NOTE: this file exists as both
|
2009-04-16 09:01:41 +00:00
|
|
|
// boinc/apps/upper_case.cpp
|
2007-07-20 18:46:43 +00:00
|
|
|
// and
|
2009-04-16 09:01:41 +00:00
|
|
|
// boinc_samples/example_app/uc2.cpp
|
2007-07-20 18:46:43 +00:00
|
|
|
// If you update one, please update the other!
|
|
|
|
|
2008-01-29 19:55:25 +00:00
|
|
|
// The program converts a mixed-case file to upper case:
|
2007-07-20 18:46:43 +00:00
|
|
|
// read "in", convert to upper case, write to "out"
|
|
|
|
//
|
2008-01-29 19:55:25 +00:00
|
|
|
// command line options
|
2011-09-20 18:49:38 +00:00
|
|
|
// --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
|
|
|
|
// --trickle_up: sent a trickle-up message
|
|
|
|
// --trickle_down: receive a trickle-up message
|
2007-07-20 18:46:43 +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"
|
|
|
|
|
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
#include "uc2.h"
|
|
|
|
UC_SHMEM* shmem;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
#define CHECKPOINT_FILE "upper_case_state"
|
|
|
|
#define INPUT_FILENAME "in"
|
|
|
|
#define OUTPUT_FILENAME "out"
|
|
|
|
|
|
|
|
bool run_slow = false;
|
|
|
|
bool early_exit = false;
|
|
|
|
bool early_crash = false;
|
|
|
|
bool early_sleep = false;
|
2011-09-20 18:49:38 +00:00
|
|
|
bool trickle_up = false;
|
|
|
|
bool trickle_down = false;
|
2008-05-14 20:32:40 +00:00
|
|
|
double cpu_time = 20, comp_result;
|
|
|
|
|
2008-05-22 04:05:04 +00:00
|
|
|
// do a billion floating-point ops
|
|
|
|
// (note: I needed to add an arg to this;
|
|
|
|
// otherwise the MS C++ compiler optimizes away
|
|
|
|
// all but the first call to it!)
|
|
|
|
//
|
|
|
|
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;
|
2007-07-20 18:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int do_checkpoint(MFILE& mf, int nchars) {
|
|
|
|
int retval;
|
|
|
|
string resolved_name;
|
|
|
|
|
|
|
|
FILE* f = fopen("temp", "w");
|
|
|
|
if (!f) return 1;
|
|
|
|
fprintf(f, "%d", nchars);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
retval = mf.flush();
|
|
|
|
if (retval) return retval;
|
|
|
|
boinc_resolve_filename_s(CHECKPOINT_FILE, resolved_name);
|
|
|
|
retval = boinc_rename("temp", resolved_name.c_str());
|
|
|
|
if (retval) return retval;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
void update_shmem() {
|
|
|
|
if (!shmem) return;
|
2008-06-10 23:29:32 +00:00
|
|
|
|
2008-07-28 02:07:11 +00:00
|
|
|
// always do this; otherwise a graphics app will immediately
|
|
|
|
// assume we're not alive
|
|
|
|
shmem->update_time = dtime();
|
|
|
|
|
2008-06-10 23:29:32 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2007-07-20 18:46:43 +00:00
|
|
|
shmem->fraction_done = boinc_get_fraction_done();
|
|
|
|
shmem->cpu_time = boinc_worker_thread_cpu_time();;
|
|
|
|
boinc_get_status(&shmem->status);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int i;
|
|
|
|
int c, nchars = 0, retval, n;
|
|
|
|
double fsize, fd;
|
2010-06-18 20:02:35 +00:00
|
|
|
char input_path[512], output_path[512], chkpt_path[512], buf[256];
|
2007-07-20 18:46:43 +00:00
|
|
|
MFILE out;
|
|
|
|
FILE* state, *infile;
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
2011-09-20 18:49:38 +00:00
|
|
|
if (strstr(argv[i], "early_exit")) early_exit = true;
|
|
|
|
if (strstr(argv[i], "early_crash")) early_crash = true;
|
|
|
|
if (strstr(argv[i], "early_sleep")) early_sleep = true;
|
|
|
|
if (strstr(argv[i], "run_slow")) run_slow = true;
|
|
|
|
if (strstr(argv[i], "cpu_time")) {
|
2007-07-20 18:46:43 +00:00
|
|
|
cpu_time = atof(argv[++i]);
|
|
|
|
}
|
2011-09-20 18:49:38 +00:00
|
|
|
if (strstr(argv[i], "trickle_up")) trickle_up = true;
|
|
|
|
if (strstr(argv[i], "trickle_down")) trickle_down = true;
|
2007-07-20 18:46:43 +00:00
|
|
|
}
|
|
|
|
|
2007-09-26 19:46:16 +00:00
|
|
|
retval = boinc_init();
|
2008-01-29 20:10:46 +00:00
|
|
|
if (retval) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s boinc_init returned %d\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), retval
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2008-01-29 20:10:46 +00:00
|
|
|
exit(retval);
|
|
|
|
}
|
2007-07-20 18:46:43 +00:00
|
|
|
|
|
|
|
// open the input file (resolve logical name first)
|
|
|
|
//
|
|
|
|
boinc_resolve_filename(INPUT_FILENAME, input_path, sizeof(input_path));
|
|
|
|
infile = boinc_fopen(input_path, "r");
|
|
|
|
if (!infile) {
|
|
|
|
fprintf(stderr,
|
2009-03-27 23:42:47 +00:00
|
|
|
"%s Couldn't find input file, resolved name %s.\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), input_path
|
2007-07-20 18:46:43 +00:00
|
|
|
);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get size of input file (used to compute fraction done)
|
|
|
|
//
|
|
|
|
file_size(input_path, fsize);
|
|
|
|
|
|
|
|
boinc_resolve_filename(OUTPUT_FILENAME, output_path, sizeof(output_path));
|
|
|
|
|
|
|
|
// See if there's a valid checkpoint file.
|
|
|
|
// If so seek input file and truncate output file
|
|
|
|
//
|
|
|
|
boinc_resolve_filename(CHECKPOINT_FILE, chkpt_path, sizeof(chkpt_path));
|
|
|
|
state = boinc_fopen(chkpt_path, "r");
|
|
|
|
if (state) {
|
|
|
|
n = fscanf(state, "%d", &nchars);
|
|
|
|
fclose(state);
|
|
|
|
}
|
|
|
|
if (state && n==1) {
|
|
|
|
fseek(infile, nchars, SEEK_SET);
|
|
|
|
boinc_truncate(output_path, nchars);
|
2008-05-08 21:16:39 +00:00
|
|
|
retval = out.open(output_path, "ab");
|
2007-07-20 18:46:43 +00:00
|
|
|
} else {
|
2008-05-08 21:16:39 +00:00
|
|
|
retval = out.open(output_path, "wb");
|
2007-07-20 18:46:43 +00:00
|
|
|
}
|
|
|
|
if (retval) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s APP: upper_case output open failed:\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
|
|
|
fprintf(stderr, "%s resolved name %s, retval %d\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), output_path, retval
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2007-07-20 18:46:43 +00:00
|
|
|
perror("open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
// create shared mem segment for graphics, and arrange to update it
|
|
|
|
//
|
|
|
|
shmem = (UC_SHMEM*)boinc_graphics_make_shmem("uppercase", sizeof(UC_SHMEM));
|
2007-09-27 04:38:47 +00:00
|
|
|
if (!shmem) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s failed to create shared mem segment\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2007-09-27 04:38:47 +00:00
|
|
|
}
|
|
|
|
update_shmem();
|
2007-07-20 18:46:43 +00:00
|
|
|
boinc_register_timer_callback(update_shmem);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// main loop - read characters, convert to UC, write
|
|
|
|
//
|
2011-06-12 20:58:43 +00:00
|
|
|
for (i=0; ; i++) {
|
2007-07-20 18:46:43 +00:00
|
|
|
c = fgetc(infile);
|
|
|
|
|
|
|
|
if (c == EOF) break;
|
|
|
|
c = toupper(c);
|
|
|
|
out._putchar(c);
|
|
|
|
nchars++;
|
|
|
|
if (run_slow) {
|
|
|
|
boinc_sleep(1.);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (early_exit && i>30) {
|
|
|
|
exit(-10);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (early_crash && i>30) {
|
|
|
|
boinc_crash();
|
|
|
|
}
|
|
|
|
if (early_sleep && i>30) {
|
2009-04-16 09:01:41 +00:00
|
|
|
g_sleep = true;
|
|
|
|
while (1) boinc_sleep(1);
|
|
|
|
}
|
2007-07-20 18:46:43 +00:00
|
|
|
|
|
|
|
if (boinc_time_to_checkpoint()) {
|
|
|
|
retval = do_checkpoint(out, nchars);
|
|
|
|
if (retval) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s APP: upper_case checkpoint failed %d\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), retval
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2007-07-20 18:46:43 +00:00
|
|
|
exit(retval);
|
|
|
|
}
|
|
|
|
boinc_checkpoint_completed();
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = nchars/fsize;
|
|
|
|
if (cpu_time) fd /= 2;
|
|
|
|
boinc_fraction_done(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = out.flush();
|
|
|
|
if (retval) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s APP: upper_case flush failed %d\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), retval
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2007-07-20 18:46:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-09-20 18:49:38 +00:00
|
|
|
if (trickle_up) {
|
2011-09-22 06:34:52 +00:00
|
|
|
boinc_send_trickle_up(
|
|
|
|
const_cast<char*>("example_app"),
|
|
|
|
const_cast<char*>("sample trickle message")
|
|
|
|
);
|
2011-09-20 18:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (trickle_down) {
|
|
|
|
boinc_sleep(10);
|
|
|
|
retval = boinc_receive_trickle_down(buf, sizeof(buf));
|
|
|
|
if (!retval) {
|
|
|
|
fprintf(stderr, "Got trickle-down message: %s\n", buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-20 18:46:43 +00:00
|
|
|
// burn up some CPU time if needed
|
|
|
|
//
|
|
|
|
if (cpu_time) {
|
|
|
|
double start = dtime();
|
2011-06-12 20:58:43 +00:00
|
|
|
for (i=0; ; i++) {
|
2007-07-20 18:46:43 +00:00
|
|
|
double e = dtime()-start;
|
|
|
|
if (e > cpu_time) break;
|
|
|
|
fd = .5 + .5*(e/cpu_time);
|
|
|
|
boinc_fraction_done(fd);
|
|
|
|
|
2009-04-16 09:01:41 +00:00
|
|
|
if (boinc_time_to_checkpoint()) {
|
|
|
|
retval = do_checkpoint(out, nchars);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "%s APP: upper_case checkpoint failed %d\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), retval
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2009-04-16 09:01:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
boinc_checkpoint_completed();
|
|
|
|
}
|
2008-05-14 20:32:40 +00:00
|
|
|
comp_result = do_a_giga_flop(i);
|
2007-07-20 18:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
boinc_fraction_done(1);
|
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
update_shmem();
|
|
|
|
#endif
|
|
|
|
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
|
|
|
|
|
2010-06-18 20:02:35 +00:00
|
|
|
const char *BOINC_RCSID_33ac47a071 = "$Id: upper_case.cpp 20315 2010-01-29 15:50:47Z davea $";
|
|
|
|
|