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

« back to all changes in this revision

Viewing changes to apps/upper_case.cpp

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
// You should have received a copy of the GNU Lesser General Public License
16
16
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17
17
 
18
 
// This is the primary sample BOINC application;
19
 
// it shows most of the features of the BOINC API.
 
18
// This program serves as both
 
19
// - An example BOINC application, illustrating the use of the BOINC API
 
20
// - A program for testing various features of BOINC
20
21
//
 
22
// NOTE: this file exists as both
 
23
// boinc/apps/upper_case.cpp
 
24
// and
 
25
// boinc_samples/example_app/uc2.cpp
 
26
// If you update one, please update the other!
 
27
 
 
28
// The program converts a mixed-case file to upper case:
21
29
// read "in", convert to upper case, write to "out"
22
30
//
23
 
// command line options (use for debugging various scenarios):
24
 
// -run_slow: sleep 1 second after each character; useful for debugging
 
31
// command line options
 
32
// -run_slow: sleep 1 second after each character
25
33
// -cpu_time N: use about N CPU seconds after copying files
26
34
// -early_exit: exit(10) after 30 chars
27
35
// -early_crash: crash after 30 chars
62
70
bool early_exit = false;
63
71
bool early_crash = false;
64
72
bool early_sleep = false;
65
 
double cpu_time = 20;
 
73
double cpu_time = 20, comp_result;
66
74
 
67
 
static void use_some_cpu() {
68
 
    double j = 3.14159;
69
 
    int i, n = 0;
70
 
    for (i=0; i<20000000; i++) {
71
 
        n++;
72
 
        j *= n+j-3.14159;
73
 
        j /= (float)n;
 
75
// do a billion floating-point ops
 
76
// (note: I needed to add an arg to this;
 
77
// otherwise the MS C++ compiler optimizes away
 
78
// all but the first call to it!)
 
79
//
 
80
static double do_a_giga_flop(int foo) {
 
81
    double x = 3.14159*foo;
 
82
    int i;
 
83
    for (i=0; i<500000000; i++) {
 
84
        x += 5.12313123;
 
85
        x *= 0.5398394834;
74
86
    }
 
87
    return x;
75
88
}
76
89
 
77
90
int do_checkpoint(MFILE& mf, int nchars) {
83
96
    fprintf(f, "%d", nchars);
84
97
    fclose(f);
85
98
 
86
 
    fprintf(stderr, "APP: upper_case checkpointing\n");
87
 
 
88
99
    retval = mf.flush();
89
100
    if (retval) return retval;
90
101
    boinc_resolve_filename_s(CHECKPOINT_FILE, resolved_name);
91
102
    retval = boinc_rename("temp", resolved_name.c_str());
92
103
    if (retval) return retval;
93
104
 
94
 
        //use_some_cpu();
95
 
    fprintf(stderr, "APP: upper_case checkpoint done\n");
96
105
    return 0;
97
106
}
98
107
 
99
108
#ifdef APP_GRAPHICS
100
109
void update_shmem() {
101
110
    if (!shmem) return;
 
111
 
 
112
    // always do this; otherwise a graphics app will immediately
 
113
    // assume we're not alive
 
114
    shmem->update_time = dtime();
 
115
 
 
116
    // Check whether a graphics app is running,
 
117
    // and don't bother updating shmem if so.
 
118
    // This doesn't matter here,
 
119
    // but may be worth doing if updating shmem is expensive.
 
120
    //
 
121
    if (shmem->countdown > 0) {
 
122
        // the graphics app sets this to 5 every time it renders a frame
 
123
        shmem->countdown--;
 
124
    } else {
 
125
        return;
 
126
    }
102
127
    shmem->fraction_done = boinc_get_fraction_done();
103
128
    shmem->cpu_time = boinc_worker_thread_cpu_time();;
104
 
    shmem->update_time = dtime();
105
129
    boinc_get_status(&shmem->status);
106
130
}
107
131
#endif
110
134
    int i;
111
135
    int c, nchars = 0, retval, n;
112
136
    double fsize, fd;
113
 
    char input_path[512], output_path[512], chkpt_path[512];
 
137
    char input_path[512], output_path[512], chkpt_path[512], buf[256];
114
138
    MFILE out;
115
139
    FILE* state, *infile;
116
140
 
125
149
    }
126
150
 
127
151
    retval = boinc_init();
128
 
    if (retval) exit(retval);
 
152
    if (retval) {
 
153
        fprintf(stderr, "%s boinc_init returned %d\n",
 
154
            boinc_msg_prefix(buf, sizeof(buf)), retval
 
155
        );
 
156
        exit(retval);
 
157
    }
129
158
 
130
159
    // open the input file (resolve logical name first)
131
160
    //
133
162
    infile = boinc_fopen(input_path, "r");
134
163
    if (!infile) {
135
164
        fprintf(stderr,
136
 
            "Couldn't find input file, resolved name %s.\n", input_path
 
165
            "%s Couldn't find input file, resolved name %s.\n",
 
166
            boinc_msg_prefix(buf, sizeof(buf)), input_path
137
167
        );
138
168
        exit(-1);
139
169
    }
156
186
    if (state && n==1) {
157
187
        fseek(infile, nchars, SEEK_SET);
158
188
        boinc_truncate(output_path, nchars);
159
 
        retval = out.open(output_path, "a");
 
189
        retval = out.open(output_path, "ab");
160
190
    } else {
161
 
        retval = out.open(output_path, "w");
 
191
        retval = out.open(output_path, "wb");
162
192
    }
163
193
    if (retval) {
164
 
        fprintf(stderr, "APP: upper_case output open failed:\n");
165
 
        fprintf(stderr, "resolved name %s, retval %d\n", output_path, retval);
 
194
        fprintf(stderr, "%s APP: upper_case output open failed:\n",
 
195
            boinc_msg_prefix(buf, sizeof(buf))
 
196
        );
 
197
        fprintf(stderr, "%s resolved name %s, retval %d\n",
 
198
            boinc_msg_prefix(buf, sizeof(buf)), output_path, retval
 
199
        );
166
200
        perror("open");
167
201
        exit(1);
168
202
    }
171
205
    // create shared mem segment for graphics, and arrange to update it
172
206
    //
173
207
    shmem = (UC_SHMEM*)boinc_graphics_make_shmem("uppercase", sizeof(UC_SHMEM));
 
208
    if (!shmem) {
 
209
        fprintf(stderr, "%s failed to create shared mem segment\n",
 
210
            boinc_msg_prefix(buf, sizeof(buf))
 
211
        );
 
212
    }
 
213
    update_shmem();
174
214
    boinc_register_timer_callback(update_shmem);
175
215
#endif
176
216
 
177
217
    // main loop - read characters, convert to UC, write
178
218
    //
179
 
    for (i=0; ; i++) {
 
219
    for (int i=0; ; i++) {
180
220
        c = fgetc(infile);
181
221
 
182
222
        if (c == EOF) break;
195
235
            boinc_crash();
196
236
        }
197
237
        if (early_sleep && i>30) {
198
 
                        g_sleep = true;
199
 
                        while (1) boinc_sleep(1);
200
 
                }
 
238
            g_sleep = true;
 
239
            while (1) boinc_sleep(1);
 
240
        }
201
241
 
202
242
        if (boinc_time_to_checkpoint()) {
203
243
            retval = do_checkpoint(out, nchars);
204
244
            if (retval) {
205
 
                fprintf(stderr, "APP: upper_case checkpoint failed %d\n", retval);
 
245
                fprintf(stderr, "%s APP: upper_case checkpoint failed %d\n",
 
246
                    boinc_msg_prefix(buf, sizeof(buf)), retval
 
247
                );
206
248
                exit(retval);
207
249
            }
208
250
            boinc_checkpoint_completed();
215
257
 
216
258
    retval = out.flush();
217
259
    if (retval) {
218
 
        fprintf(stderr, "APP: upper_case flush failed %d\n", retval);
 
260
        fprintf(stderr, "%s APP: upper_case flush failed %d\n",
 
261
            boinc_msg_prefix(buf, sizeof(buf)), retval
 
262
        );
219
263
        exit(1);
220
264
    }
221
265
 
223
267
    //
224
268
    if (cpu_time) {
225
269
        double start = dtime();
226
 
        while (1) {
 
270
        for (int i=0; ; i++) {
227
271
            double e = dtime()-start;
228
272
            if (e > cpu_time) break;
229
273
            fd = .5 + .5*(e/cpu_time);
230
274
            boinc_fraction_done(fd);
231
275
 
232
 
                        if (boinc_time_to_checkpoint()) {
233
 
                                retval = do_checkpoint(out, nchars);
234
 
                                if (retval) {
235
 
                                        fprintf(stderr, "APP: upper_case checkpoint failed %d\n", retval);
236
 
                                        exit(1);
237
 
                                }
238
 
                                boinc_checkpoint_completed();
239
 
                        }
240
 
 
241
 
            use_some_cpu();
 
276
            if (boinc_time_to_checkpoint()) {
 
277
                retval = do_checkpoint(out, nchars);
 
278
                if (retval) {
 
279
                    fprintf(stderr, "%s APP: upper_case checkpoint failed %d\n",
 
280
                        boinc_msg_prefix(buf, sizeof(buf)), retval
 
281
                    );
 
282
                    exit(1);
 
283
                }
 
284
                boinc_checkpoint_completed();
 
285
            }
 
286
            comp_result = do_a_giga_flop(i);
242
287
        }
243
288
    }
244
289
    boinc_fraction_done(1);
260
305
}
261
306
#endif
262
307
 
263
 
const char *BOINC_RCSID_33ac47a071 = "$Id: upper_case.cpp 16068 2008-09-26 18:10:10Z davea $";
 
308
const char *BOINC_RCSID_33ac47a071 = "$Id: upper_case.cpp 22363 2010-09-15 23:03:30Z davea $";
264
309