LLVM OpenMP 19.0.0git
redetect.c
Go to the documentation of this file.
1// RUN: %libomp-compile
2// RUN: %libomp-run
3// RUN: env KMP_AFFINITY=none %libomp-run
4// REQUIRES: linux
5
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
9
10#define _GNU_SOURCE
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>
18
19// Set the affinity mask of the calling thread to a proper subset of the
20// original affinity mask, specifically, one processor less.
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)) {
29 break;
30 }
31 }
32 affinity_mask_free(original_mask);
34}
35
36int 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;
43 n = affinity_mask_snprintf(buf, sizeof(buf), mask);
44 printf("Orignal Mask: %s\n", buf);
45
46 if (affinity_mask_count(mask) == 1) {
47 printf("Only one processor in affinity mask, skipping test.\n");
48 exit(EXIT_SUCCESS);
49 }
50
51 #pragma omp parallel
52 {
53 #pragma omp single
54 printf("Hello! Thread %d executed single region in parent process\n",
55 omp_get_thread_num());
56 }
57
58 pid_t pid = fork();
59 if (pid < 0) {
60 perror("fork()");
61 exit(EXIT_FAILURE);
62 }
63
64 if (pid == 0) {
65 // Let child set a new initial mask
67 #pragma omp parallel
68 {
69 #pragma omp single
70 printf("Hello! Thread %d executed single region in child process\n",
71 omp_get_thread_num());
72 }
74 get_thread_affinity(new_mask);
75 if (!affinity_mask_equal(mask, new_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);
83 }
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);
92 }
93 if (WIFEXITED(child_exit_status)) {
94 exit_status = WEXITSTATUS(child_exit_status);
95 } else {
96 exit_status = EXIT_FAILURE;
97 }
98 }
99
101 return exit_status;
102}
char buf[BUFFER_SIZE]
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int mask
static void affinity_mask_copy(affinity_mask_t *dest, const affinity_mask_t *src)
static int affinity_mask_isset(const affinity_mask_t *mask, int cpu)
static affinity_mask_t * affinity_mask_alloc()
static int affinity_mask_count(const affinity_mask_t *mask)
#define AFFINITY_MAX_CPUS
static void set_thread_affinity(const affinity_mask_t *mask)
static int affinity_mask_equal(const affinity_mask_t *mask1, const affinity_mask_t *mask2)
static void affinity_mask_clr(affinity_mask_t *mask, int cpu)
static void affinity_mask_free(affinity_mask_t *mask)
static size_t affinity_mask_snprintf(char *buf, size_t bufsize, const affinity_mask_t *mask)
static void get_thread_affinity(affinity_mask_t *mask)
void set_subset_affinity(affinity_mask_t *mask)
Definition: redetect.c:21
int main()
Definition: test-touch.c:21