1 // RUN: %libomp-compile
2 // RUN: env KMP_AFFINITY=none %libomp-run
5 // Check if forked child process resets affinity properly by restricting
6 // child's affinity to a subset of the parent and then checking it after
10 #include "libomp_test_affinity.h"
15 #include <sys/types.h>
18 // Set the affinity mask of the calling thread to a proper subset of the
19 // original affinity mask, specifically, one processor less.
20 void set_subset_affinity(affinity_mask_t
*mask
) {
22 affinity_mask_t
*original_mask
= affinity_mask_alloc();
23 affinity_mask_copy(original_mask
, mask
);
24 // Find first processor to clear for subset mask
25 for (cpu
= 0; cpu
<= AFFINITY_MAX_CPUS
; ++cpu
) {
26 if (affinity_mask_isset(original_mask
, cpu
)) {
27 affinity_mask_clr(mask
, cpu
);
31 affinity_mask_free(original_mask
);
32 set_thread_affinity(mask
);
35 int main(int argc
, char **argv
) {
39 int child_exit_status
, exit_status
;
40 affinity_mask_t
*mask
= affinity_mask_alloc();
41 get_thread_affinity(mask
);
42 n
= affinity_mask_snprintf(buf
, sizeof(buf
), mask
);
43 printf("Orignal Mask: %s\n", buf
);
45 if (affinity_mask_count(mask
) == 1) {
46 printf("Only one processor in affinity mask, skipping test.\n");
53 printf("Hello! Thread %d executed single region in parent process\n",
54 omp_get_thread_num());
64 // Let child set a new initial mask
65 set_subset_affinity(mask
);
69 printf("Hello! Thread %d executed single region in child process\n",
70 omp_get_thread_num());
72 affinity_mask_t
*new_mask
= affinity_mask_alloc();
73 get_thread_affinity(new_mask
);
74 if (!affinity_mask_equal(mask
, new_mask
)) {
75 affinity_mask_snprintf(buf
, sizeof(buf
), mask
);
76 fprintf(stderr
, "Original Mask = %s\n", buf
);
77 affinity_mask_snprintf(buf
, sizeof(buf
), new_mask
);
78 fprintf(stderr
, "New Mask = %s\n", buf
);
79 affinity_mask_free(new_mask
);
80 fprintf(stderr
, "Child affinity mask did not reset properly\n");
83 affinity_mask_free(new_mask
);
84 exit_status
= EXIT_SUCCESS
;
86 pid_t child_pid
= pid
;
87 pid
= wait(&child_exit_status
);
92 if (WIFEXITED(child_exit_status
)) {
93 exit_status
= WEXITSTATUS(child_exit_status
);
95 exit_status
= EXIT_FAILURE
;
99 affinity_mask_free(mask
);