1/*
2 * Copyright (c) 2009, 2010, 2011, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#include <assert.h>
11#include <stdbool.h>
12#include <stdlib.h>
13#include <stdio.h>
14#include <time.h>
15#include <assert.h>
16#include <stdint.h>
17#include <omp.h>
18#include <barrelfish/barrelfish.h>
19#include <bench/bench.h>
20#include <trace/trace.h>
21#include <trace_definitions/trace_defs.h>
22#include <inttypes.h>
23
24#define STACK_SIZE      (64 * 1024)
25
26int main(int argc, char *argv[])
27{
28    volatile uint64_t workcnt = 0;
29    int nthreads;
30
31    debug_printf("bomptest started.\n");
32
33    bench_init();
34
35#if CONFIG_TRACE
36    errval_t err = trace_control(TRACE_EVENT(TRACE_SUBSYS_ROUTE,
37                                             TRACE_EVENT_ROUTE_BENCH_START, 0),
38                                 TRACE_EVENT(TRACE_SUBSYS_ROUTE,
39                                             TRACE_EVENT_ROUTE_BENCH_STOP, 0), 0);
40    assert(err_is_ok(err));
41#endif
42
43    if(argc == 2) {
44        nthreads = atoi(argv[1]);
45        bomp_bomp_init(nthreads);
46        omp_set_num_threads(nthreads);
47    } else {
48        assert(!"Specify number of threads");
49    }
50
51    trace_event(TRACE_SUBSYS_ROUTE, TRACE_EVENT_ROUTE_BENCH_START, 0);
52
53    uint64_t start = bench_tsc();
54    debug_printf("bomp_test: parallel loop");
55#pragma omp parallel
56    while(rdtsc() < start + 805000000ULL) {
57        workcnt++;
58    }
59    uint64_t end = bench_tsc();
60
61    trace_event(TRACE_SUBSYS_ROUTE, TRACE_EVENT_ROUTE_BENCH_STOP, 0);
62
63    printf("done. time taken: %" PRIu64 " cycles.\n", end - start);
64
65    uint32_t *src = calloc(1024, sizeof(uint32_t));
66    uint32_t *dst = calloc(1024, sizeof(uint32_t));
67
68    printf("Test 2...\n");
69
70    for (int i = 0; i < 1024; ++i) {
71        src[i] = i+1;
72    }
73    for (int i = 0; i < 1024; ++i) {
74        assert(src[i] != dst[i]);
75    }
76
77    printf("parallel for..\n");
78    #pragma omp parallel
79    for (int i = 0; i < 1024; ++i) {
80        dst[i] = src[i];
81    }
82
83    printf("Verification...");
84    for (int i = 0; i < 1024; ++i) {
85        assert(src[i] == dst[i]);
86    }
87
88    printf("OK.\n");
89
90
91#if CONFIG_TRACE
92        char *buf = malloc(4096*4096);
93        trace_dump(buf, 4096*4096, NULL);
94        printf("%s\n", buf);
95#endif
96
97    for(;;);
98    return 0;
99}
100