File: variable_num_threads_aottest.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (51 lines) | stat: -rw-r--r-- 1,277 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "HalideBuffer.h"
#include "HalideRuntime.h"

#include <algorithm>
#include <atomic>
#include <stdio.h>
#include <stdlib.h>

#include "variable_num_threads.h"

std::atomic<bool> stop{false};
std::atomic<int> max_threads{1};

using namespace Halide::Runtime;

void mess_with_num_threads(void *) {
    while (!stop) {
        halide_set_num_threads((rand() % max_threads) + 1);
    }
}

int main(int argc, char **argv) {

    halide_set_num_threads(1);

    // In one thread we'll run a job with lots of nested parallelism,
    // and in another we'll mess with the number of threads we want
    // running. The intent is to hunt for deadlocks.

    halide_thread *t = halide_spawn_thread(&mess_with_num_threads, nullptr);

    Buffer<float, 2> out(64, 64);

    for (int i = 0; i < 1000; i++) {
        // The number of threads will oscillate randomly, but the range
        // will slowly ramp up and back down so you can watch it
        // working in a process monitor.
        max_threads = 1 + std::min(i, 1000 - i) / 50;
        int ret = variable_num_threads(out);
        if (ret) {
            printf("Non zero exit code: %d\n", ret);
            return 1;
        }
    }

    stop = true;
    halide_join_thread(t);

    printf("Success!\n");
    return 0;
}