source: tests/unified_locking/thread_test.cfa @ 6ff08d8

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 6ff08d8 was 6ff08d8, checked in by caparsons <caparson@…>, 3 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 2.8 KB
Line 
1#include <stdio.h>
2#include "locks.hfa"
3#include <stdlib.hfa>
4#include <thread.hfa>
5#include <containers/array.hfa>
6
7static unsigned int taskCount = 4;
8static unsigned int threadCount = 2;
9static unsigned int lockCount = 1;
10static unsigned int total_times = 320000;
11static unsigned int num_times;
12static const int workBufferSize = 16;
13static unsigned int work_unlocked = 10000;
14static unsigned int work_locked = 10000;
15
16// taken from martin's thread_test
17static inline void dowork(volatile int* buffer, unsigned int steps) {
18  int value = 0;
19  for (unsigned int i = 0; i < steps; i += 1) {
20    // a little more work than just a single memory access helps with stability
21    value += (buffer[i % workBufferSize] * 17) / 23 + 55;
22  }
23  buffer[0] += value;
24}
25
26thread worker {
27    linear_backoff_then_block_lock * locks;
28    bool improved;
29};
30
31void ?{}( worker & w, linear_backoff_then_block_lock * locks, bool improved ) {
32        w.locks = locks;
33    w.improved = improved;
34}
35
36
37void main( worker & this ) with(this) {
38        int buffer[workBufferSize];
39    for (int i = 0; i < workBufferSize; i += 1) buffer[i] = rand() % 1024;
40    unsigned int lck = rand() % lockCount;
41    linear_backoff_then_block_lock * curr_lock = &locks[lck];
42    for (unsigned int i = 0; i < num_times; i++) {
43        dowork(buffer, work_unlocked);
44        if (improved) lock_improved(*curr_lock);
45        else lock(*curr_lock);
46        dowork(buffer, work_locked);
47        unlock(*curr_lock);
48        lck = rand() % lockCount;
49        curr_lock = &locks[lck];
50    }
51}
52
53
54int main(int argc, char* argv[]) {
55    switch (argc) {
56        case 7:
57            work_unlocked = atoi(argv[5]);
58        case 6:
59            work_locked = atoi(argv[5]);
60        case 5:
61            num_times = atoi(argv[4]);
62        case 4:
63            lockCount = atoi(argv[3]);
64        case 3:
65            threadCount = atoi(argv[2]);
66        case 2:
67            taskCount = atoi(argv[1]);
68        case 1:
69            break;
70        default:
71            break;
72    }
73        processor p[threadCount];
74    linear_backoff_then_block_lock locks[lockCount];
75    worker * worker_arr[taskCount];
76    num_times = total_times  / taskCount;
77
78        //printf("Start Test: martin lock simple\n");
79        clock_t begin = clock();
80        for (unsigned int i = 0; i < taskCount; i++) {
81        worker_arr[i] = new( locks, false );
82    }
83    for (unsigned int i = 0; i < taskCount; i++) {
84        delete( worker_arr[i] );
85    }
86        clock_t end = clock();
87        double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
88        printf("norm: %f\n", time_spent);
89
90    //printf("Start Test: martin lock improved\n");
91        begin = clock();
92        for (unsigned int i = 0; i < taskCount; i++) {
93        worker_arr[i] = new( locks, true );
94    }
95    for (unsigned int i = 0; i < taskCount; i++) {
96        delete( worker_arr[i] );
97    }
98        end = clock();
99        time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
100        printf("improved: %f\n", time_spent);
101}
Note: See TracBrowser for help on using the repository browser.