2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2003-07-15 22:13:38 +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.
|
2003-07-15 22:13:38 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +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.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
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/>.
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2010-01-29 15:50:47 +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:47:52 +00:00
|
|
|
//
|
2010-01-29 15:50:47 +00:00
|
|
|
// NOTE: this file exists as both
|
|
|
|
// boinc/apps/upper_case.cpp
|
|
|
|
// and
|
|
|
|
// boinc_samples/example_app/uc2.cpp
|
|
|
|
// If you update one, please update the other!
|
|
|
|
|
|
|
|
// The program converts a mixed-case file to upper case:
|
2007-07-20 18:47:52 +00:00
|
|
|
// read "in", convert to upper case, write to "out"
|
|
|
|
//
|
2010-01-29 15:50:47 +00:00
|
|
|
// command line options
|
|
|
|
// -run_slow: sleep 1 second after each character
|
2007-07-20 18:47:52 +00:00
|
|
|
// -cpu_time N: use about N CPU seconds after copying files
|
|
|
|
// -early_exit: exit(10) after 30 chars
|
|
|
|
// -early_crash: crash after 30 chars
|
2002-09-04 18:18:40 +00:00
|
|
|
//
|
2006-04-14 17:55:02 +00:00
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:29:48 +00:00
|
|
|
#include "boinc_win.h"
|
2005-11-21 18:34:44 +00:00
|
|
|
#else
|
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cctype>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <csignal>
|
2002-10-24 17:06:42 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
#include "str_util.h"
|
2002-09-04 22:14:20 +00:00
|
|
|
#include "util.h"
|
2002-09-04 18:18:40 +00:00
|
|
|
#include "filesys.h"
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "boinc_api.h"
|
2004-04-15 00:21:29 +00:00
|
|
|
#include "mfile.h"
|
2007-07-20 18:47:52 +00:00
|
|
|
#include "graphics2.h"
|
|
|
|
|
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
#include "uc2.h"
|
|
|
|
UC_SHMEM* shmem;
|
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-06-30 18:17:21 +00:00
|
|
|
using std::string;
|
|
|
|
|
2002-09-04 18:18:40 +00:00
|
|
|
#define CHECKPOINT_FILE "upper_case_state"
|
2005-04-28 23:19:58 +00:00
|
|
|
#define INPUT_FILENAME "in"
|
|
|
|
#define OUTPUT_FILENAME "out"
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
bool run_slow = false;
|
|
|
|
bool early_exit = false;
|
|
|
|
bool early_crash = false;
|
|
|
|
bool early_sleep = false;
|
2010-01-29 15:50:47 +00:00
|
|
|
double cpu_time = 20, comp_result;
|
|
|
|
|
|
|
|
// 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;
|
2007-07-20 18:47:52 +00:00
|
|
|
}
|
2010-01-29 15:50:47 +00:00
|
|
|
return x;
|
2007-07-20 18:47:52 +00:00
|
|
|
}
|
2002-09-04 18:18:40 +00:00
|
|
|
|
|
|
|
int do_checkpoint(MFILE& mf, int nchars) {
|
|
|
|
int retval;
|
2003-11-08 00:56:55 +00:00
|
|
|
string resolved_name;
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2003-11-08 00:56:55 +00:00
|
|
|
FILE* f = fopen("temp", "w");
|
2002-09-04 18:18:40 +00:00
|
|
|
if (!f) return 1;
|
|
|
|
fprintf(f, "%d", nchars);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
retval = mf.flush();
|
|
|
|
if (retval) return retval;
|
2004-06-08 14:37:42 +00:00
|
|
|
boinc_resolve_filename_s(CHECKPOINT_FILE, resolved_name);
|
2003-11-08 00:56:55 +00:00
|
|
|
retval = boinc_rename("temp", resolved_name.c_str());
|
2002-09-04 18:18:40 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
void update_shmem() {
|
|
|
|
if (!shmem) return;
|
2010-01-29 15:50:47 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
shmem->fraction_done = boinc_get_fraction_done();
|
|
|
|
shmem->cpu_time = boinc_worker_thread_cpu_time();;
|
|
|
|
boinc_get_status(&shmem->status);
|
2002-09-04 18:18:40 +00:00
|
|
|
}
|
2002-08-14 23:02:32 +00:00
|
|
|
#endif
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
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];
|
2003-09-05 21:26:21 +00:00
|
|
|
MFILE out;
|
2007-07-20 18:47:52 +00:00
|
|
|
FILE* state, *infile;
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-early_exit")) early_exit = true;
|
|
|
|
if (!strcmp(argv[i], "-early_crash")) early_crash = true;
|
|
|
|
if (!strcmp(argv[i], "-early_sleep")) early_sleep = true;
|
|
|
|
if (!strcmp(argv[i], "-run_slow")) run_slow = true;
|
|
|
|
if (!strcmp(argv[i], "-cpu_time")) {
|
|
|
|
cpu_time = atof(argv[++i]);
|
|
|
|
}
|
|
|
|
}
|
2002-06-20 23:35:44 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
retval = boinc_init();
|
2010-01-29 15:50:47 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "%s boinc_init returned %d\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), retval
|
2010-01-29 15:50:47 +00:00
|
|
|
);
|
|
|
|
exit(retval);
|
|
|
|
}
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2007-07-20 18:47:52 +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,
|
2010-01-29 15:50: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
|
2003-09-04 00:18:27 +00:00
|
|
|
);
|
2002-10-30 04:30:28 +00:00
|
|
|
exit(-1);
|
2002-10-29 18:58:42 +00:00
|
|
|
}
|
2003-09-04 00:18:27 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
// 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));
|
2003-11-10 06:49:45 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
// 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");
|
2002-09-04 18:18:40 +00:00
|
|
|
if (state) {
|
2007-07-20 18:47:52 +00:00
|
|
|
n = fscanf(state, "%d", &nchars);
|
|
|
|
fclose(state);
|
|
|
|
}
|
|
|
|
if (state && n==1) {
|
|
|
|
fseek(infile, nchars, SEEK_SET);
|
|
|
|
boinc_truncate(output_path, nchars);
|
2010-01-29 15:50:47 +00:00
|
|
|
retval = out.open(output_path, "ab");
|
2002-09-04 18:18:40 +00:00
|
|
|
} else {
|
2010-01-29 15:50:47 +00:00
|
|
|
retval = out.open(output_path, "wb");
|
2002-09-04 18:18:40 +00:00
|
|
|
}
|
|
|
|
if (retval) {
|
2010-01-29 15:50: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))
|
2010-01-29 15:50: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
|
2010-01-29 15:50:47 +00:00
|
|
|
);
|
2003-09-04 00:18:27 +00:00
|
|
|
perror("open");
|
2002-09-04 18:18:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
|
|
|
|
#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));
|
2010-01-29 15:50:47 +00:00
|
|
|
if (!shmem) {
|
|
|
|
fprintf(stderr, "%s failed to create shared mem segment\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
2010-01-29 15:50:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
update_shmem();
|
2007-07-20 18:47:52 +00:00
|
|
|
boinc_register_timer_callback(update_shmem);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// main loop - read characters, convert to UC, write
|
|
|
|
//
|
2010-01-29 15:50:47 +00:00
|
|
|
for (int i=0; ; i++) {
|
2007-07-20 18:47:52 +00:00
|
|
|
c = fgetc(infile);
|
2003-09-05 21:26:21 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
if (c == EOF) break;
|
|
|
|
c = toupper(c);
|
2002-09-04 18:18:40 +00:00
|
|
|
out._putchar(c);
|
|
|
|
nchars++;
|
|
|
|
if (run_slow) {
|
2003-03-11 22:18:01 +00:00
|
|
|
boinc_sleep(1.);
|
2002-09-04 18:18:40 +00:00
|
|
|
}
|
2003-06-11 23:09:11 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
if (early_exit && i>30) {
|
|
|
|
exit(-10);
|
2002-10-28 23:06:44 +00:00
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
|
|
|
|
if (early_crash && i>30) {
|
|
|
|
boinc_crash();
|
2003-06-28 00:10:47 +00:00
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
if (early_sleep && i>30) {
|
2010-01-29 15:50:47 +00:00
|
|
|
g_sleep = true;
|
|
|
|
while (1) boinc_sleep(1);
|
|
|
|
}
|
2002-10-28 23:06:44 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
if (boinc_time_to_checkpoint()) {
|
2002-09-04 18:18:40 +00:00
|
|
|
retval = do_checkpoint(out, nchars);
|
|
|
|
if (retval) {
|
2010-01-29 15:50: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
|
2010-01-29 15:50:47 +00:00
|
|
|
);
|
2007-07-20 18:47:52 +00:00
|
|
|
exit(retval);
|
2002-09-04 18:18:40 +00:00
|
|
|
}
|
2002-08-24 00:41:25 +00:00
|
|
|
boinc_checkpoint_completed();
|
2002-06-24 21:29:05 +00:00
|
|
|
}
|
2003-11-10 06:49:45 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
fd = nchars/fsize;
|
|
|
|
if (cpu_time) fd /= 2;
|
|
|
|
boinc_fraction_done(fd);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
|
2002-09-04 18:18:40 +00:00
|
|
|
retval = out.flush();
|
|
|
|
if (retval) {
|
2010-01-29 15:50: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
|
2010-01-29 15:50:47 +00:00
|
|
|
);
|
2002-09-04 18:18:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
|
|
|
|
// burn up some CPU time if needed
|
|
|
|
//
|
2006-01-09 23:03:15 +00:00
|
|
|
if (cpu_time) {
|
|
|
|
double start = dtime();
|
2010-01-29 15:50:47 +00:00
|
|
|
for (int i=0; ; i++) {
|
2007-07-20 18:47:52 +00:00
|
|
|
double e = dtime()-start;
|
|
|
|
if (e > cpu_time) break;
|
|
|
|
fd = .5 + .5*(e/cpu_time);
|
|
|
|
boinc_fraction_done(fd);
|
|
|
|
|
2010-01-29 15:50:47 +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
|
2010-01-29 15:50:47 +00:00
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
boinc_checkpoint_completed();
|
|
|
|
}
|
|
|
|
comp_result = do_a_giga_flop(i);
|
2006-01-09 23:03:15 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
boinc_fraction_done(1);
|
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
update_shmem();
|
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
boinc_finish(0);
|
2006-01-09 23:03:15 +00:00
|
|
|
}
|
2003-06-11 23:09:11 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
|
|
|
|
LPSTR command_line;
|
|
|
|
char* argv[100];
|
|
|
|
int argc;
|
2006-01-09 23:03:15 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
command_line = GetCommandLine();
|
|
|
|
argc = parse_command_line( command_line, argv );
|
|
|
|
return main(argc, argv);
|
2002-09-18 21:54:36 +00:00
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
#endif
|
2002-09-18 22:31:36 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_33ac47a071 = "$Id$";
|
2007-07-20 18:47:52 +00:00
|
|
|
|