1 // RUN: %libomp-compile
3 // RUN: env KMP_AFFINITY=none %libomp-run
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
11 #include "libomp_test_affinity.h"
16 #include <sys/types.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
) {
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
);
32 affinity_mask_free(original_mask
);
33 set_thread_affinity(mask
);
36 int main(int argc
, char **argv
) {
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");
54 printf("Hello! Thread %d executed single region in parent process\n",
55 omp_get_thread_num());
65 // Let child set a new initial mask
66 set_subset_affinity(mask
);
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");
84 affinity_mask_free(new_mask
);
85 exit_status
= EXIT_SUCCESS
;
87 pid_t child_pid
= pid
;
88 pid
= wait(&child_exit_status
);
93 if (WIFEXITED(child_exit_status
)) {
94 exit_status
= WEXITSTATUS(child_exit_status
);
96 exit_status
= EXIT_FAILURE
;
100 affinity_mask_free(mask
);