| 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 |
| 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> |
| 18 | |
| 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(dest: original_mask, src: mask); |
| 25 | // Find first processor to clear for subset mask |
| 26 | for (cpu = 0; cpu <= AFFINITY_MAX_CPUS; ++cpu) { |
| 27 | if (affinity_mask_isset(mask: original_mask, cpu)) { |
| 28 | affinity_mask_clr(mask, cpu); |
| 29 | break; |
| 30 | } |
| 31 | } |
| 32 | affinity_mask_free(mask: original_mask); |
| 33 | set_thread_affinity(mask); |
| 34 | } |
| 35 | |
| 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, bufsize: sizeof(buf), mask); |
| 44 | printf(format: "Orignal Mask: %s\n" , buf); |
| 45 | |
| 46 | if (affinity_mask_count(mask) == 1) { |
| 47 | printf(format: "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(format: "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(s: "fork()" ); |
| 61 | exit(EXIT_FAILURE); |
| 62 | } |
| 63 | |
| 64 | if (pid == 0) { |
| 65 | // Let child set a new initial mask |
| 66 | set_subset_affinity(mask); |
| 67 | #pragma omp parallel |
| 68 | { |
| 69 | #pragma omp single |
| 70 | printf(format: "Hello! Thread %d executed single region in child process\n" , |
| 71 | omp_get_thread_num()); |
| 72 | } |
| 73 | affinity_mask_t *new_mask = affinity_mask_alloc(); |
| 74 | get_thread_affinity(mask: new_mask); |
| 75 | if (!affinity_mask_equal(mask1: mask, mask2: new_mask)) { |
| 76 | affinity_mask_snprintf(buf, bufsize: sizeof(buf), mask); |
| 77 | fprintf(stderr, format: "Original Mask = %s\n" , buf); |
| 78 | affinity_mask_snprintf(buf, bufsize: sizeof(buf), mask: new_mask); |
| 79 | fprintf(stderr, format: "New Mask = %s\n" , buf); |
| 80 | affinity_mask_free(mask: new_mask); |
| 81 | fprintf(stderr, format: "Child affinity mask did not reset properly\n" ); |
| 82 | exit(EXIT_FAILURE); |
| 83 | } |
| 84 | affinity_mask_free(mask: new_mask); |
| 85 | exit_status = EXIT_SUCCESS; |
| 86 | } else { |
| 87 | pid_t child_pid = pid; |
| 88 | pid = wait(stat_loc: &child_exit_status); |
| 89 | if (pid == -1) { |
| 90 | perror(s: "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 | |
| 100 | affinity_mask_free(mask); |
| 101 | return exit_status; |
| 102 | } |
| 103 | |