[OpenACC] Implement 'collapse' for combined constructs.
[llvm-project.git] / openmp / runtime / test / affinity / redetect.c
blob4b96d1bd92ee7695e93d00b0241e302b07616a8b
1 // RUN: %libomp-compile
2 // RUN: %libomp-run
3 // RUN: env KMP_AFFINITY=none %libomp-run
4 // REQUIRES: linux
6 // Check if forked child process resets affinity properly by restricting
7 // child's affinity to a subset of the parent and then checking it after
8 // a parallel region
10 #define _GNU_SOURCE
11 #include "libomp_test_affinity.h"
12 #include <omp.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/wait.h>
16 #include <sys/types.h>
17 #include <unistd.h>
19 // Set the affinity mask of the calling thread to a proper subset of the
20 // original affinity mask, specifically, one processor less.
21 void set_subset_affinity(affinity_mask_t *mask) {
22 int cpu;
23 affinity_mask_t *original_mask = affinity_mask_alloc();
24 affinity_mask_copy(original_mask, mask);
25 // Find first processor to clear for subset mask
26 for (cpu = 0; cpu <= AFFINITY_MAX_CPUS; ++cpu) {
27 if (affinity_mask_isset(original_mask, cpu)) {
28 affinity_mask_clr(mask, cpu);
29 break;
32 affinity_mask_free(original_mask);
33 set_thread_affinity(mask);
36 int main(int argc, char **argv) {
37 char buf[1024] = {0};
38 char *other_buf;
39 size_t n;
40 int child_exit_status, exit_status;
41 affinity_mask_t *mask = affinity_mask_alloc();
42 get_thread_affinity(mask);
43 n = affinity_mask_snprintf(buf, sizeof(buf), mask);
44 printf("Orignal Mask: %s\n", buf);
46 if (affinity_mask_count(mask) == 1) {
47 printf("Only one processor in affinity mask, skipping test.\n");
48 exit(EXIT_SUCCESS);
51 #pragma omp parallel
53 #pragma omp single
54 printf("Hello! Thread %d executed single region in parent process\n",
55 omp_get_thread_num());
58 pid_t pid = fork();
59 if (pid < 0) {
60 perror("fork()");
61 exit(EXIT_FAILURE);
64 if (pid == 0) {
65 // Let child set a new initial mask
66 set_subset_affinity(mask);
67 #pragma omp parallel
69 #pragma omp single
70 printf("Hello! Thread %d executed single region in child process\n",
71 omp_get_thread_num());
73 affinity_mask_t *new_mask = affinity_mask_alloc();
74 get_thread_affinity(new_mask);
75 if (!affinity_mask_equal(mask, new_mask)) {
76 affinity_mask_snprintf(buf, sizeof(buf), mask);
77 fprintf(stderr, "Original Mask = %s\n", buf);
78 affinity_mask_snprintf(buf, sizeof(buf), new_mask);
79 fprintf(stderr, "New Mask = %s\n", buf);
80 affinity_mask_free(new_mask);
81 fprintf(stderr, "Child affinity mask did not reset properly\n");
82 exit(EXIT_FAILURE);
84 affinity_mask_free(new_mask);
85 exit_status = EXIT_SUCCESS;
86 } else {
87 pid_t child_pid = pid;
88 pid = wait(&child_exit_status);
89 if (pid == -1) {
90 perror("wait()");
91 exit(EXIT_FAILURE);
93 if (WIFEXITED(child_exit_status)) {
94 exit_status = WEXITSTATUS(child_exit_status);
95 } else {
96 exit_status = EXIT_FAILURE;
100 affinity_mask_free(mask);
101 return exit_status;