| 
 | 1 | +/*  | 
 | 2 | + * Copyright (c) 2015      Intel, Inc. All rights reserved  | 
 | 3 | + * $COPYRIGHT$  | 
 | 4 | + *   | 
 | 5 | + * Additional copyrights may follow  | 
 | 6 | + *   | 
 | 7 | + * $HEADER$  | 
 | 8 | + */  | 
 | 9 | + | 
 | 10 | +#include "orte_config.h"  | 
 | 11 | +#include "orte/constants.h"  | 
 | 12 | +#include "orte/types.h"  | 
 | 13 | + | 
 | 14 | +#include <errno.h>  | 
 | 15 | +#ifdef HAVE_UNISTD_H  | 
 | 16 | +#include <unistd.h>  | 
 | 17 | +#endif  /* HAVE_UNISTD_H */  | 
 | 18 | +#ifdef HAVE_STRING_H  | 
 | 19 | +#include <string.h>  | 
 | 20 | +#endif  /* HAVE_STRING_H */  | 
 | 21 | + | 
 | 22 | +#include "opal/mca/hwloc/hwloc.h"  | 
 | 23 | +#include "opal/util/argv.h"  | 
 | 24 | +#include "opal/util/opal_environ.h"  | 
 | 25 | + | 
 | 26 | +#include "orte/util/show_help.h"  | 
 | 27 | +#include "orte/util/error_strings.h"  | 
 | 28 | +#include "orte/runtime/orte_globals.h"  | 
 | 29 | +#include "orte/mca/errmgr/errmgr.h"  | 
 | 30 | +#include "orte/mca/rmaps/rmaps_types.h"  | 
 | 31 | + | 
 | 32 | +#include "orte/mca/rtc/base/base.h"  | 
 | 33 | +#include "rtc_omp.h"  | 
 | 34 | + | 
 | 35 | +static int init(void);  | 
 | 36 | +static void finalize(void);  | 
 | 37 | +static void set(orte_job_t *jdata,  | 
 | 38 | +                orte_proc_t *proc,  | 
 | 39 | +                char ***environ_copy,  | 
 | 40 | +                int write_fd);  | 
 | 41 | + | 
 | 42 | +orte_rtc_base_module_t orte_rtc_omp_module = {  | 
 | 43 | +    init,  | 
 | 44 | +    finalize,  | 
 | 45 | +    NULL,  | 
 | 46 | +    set,  | 
 | 47 | +    NULL  | 
 | 48 | +};  | 
 | 49 | + | 
 | 50 | +static int init(void)  | 
 | 51 | +{  | 
 | 52 | +    return ORTE_SUCCESS;  | 
 | 53 | +}  | 
 | 54 | + | 
 | 55 | +static void finalize(void)  | 
 | 56 | +{  | 
 | 57 | +    return;  | 
 | 58 | +}  | 
 | 59 | + | 
 | 60 | +static void set(orte_job_t *jobdat,  | 
 | 61 | +                orte_proc_t *child,  | 
 | 62 | +                char ***environ_copy,  | 
 | 63 | +                int write_fd)  | 
 | 64 | +{  | 
 | 65 | +    char *param;  | 
 | 66 | +    char *cpu_bitmap;  | 
 | 67 | +    char **ranges, *ptr, *tmp, **newrange, **results;  | 
 | 68 | +    int i, start, end;  | 
 | 69 | +      | 
 | 70 | +    opal_output_verbose(2, orte_rtc_base_framework.framework_output,  | 
 | 71 | +                        "%s hwloc:set on child %s",  | 
 | 72 | +                        ORTE_NAME_PRINT(ORTE_PROC_MY_NAME),  | 
 | 73 | +                        (NULL == child) ? "NULL" : ORTE_NAME_PRINT(&child->name));  | 
 | 74 | + | 
 | 75 | +    if (NULL == jobdat || NULL == child) {  | 
 | 76 | +        /* nothing for us to do */  | 
 | 77 | +        opal_output_verbose(2, orte_rtc_base_framework.framework_output,  | 
 | 78 | +                            "%s hwloc:set jobdat %s child %s - nothing to do",  | 
 | 79 | +                            ORTE_NAME_PRINT(ORTE_PROC_MY_NAME),  | 
 | 80 | +                            (NULL == jobdat) ? "NULL" : ORTE_JOBID_PRINT(jobdat->jobid),  | 
 | 81 | +                            (NULL == child) ? "NULL" : ORTE_NAME_PRINT(&child->name));  | 
 | 82 | +        return;  | 
 | 83 | +    }  | 
 | 84 | + | 
 | 85 | +    /* See if we are bound */  | 
 | 86 | +    cpu_bitmap = NULL;  | 
 | 87 | +    if (!orte_get_attribute(&child->attributes, ORTE_PROC_CPU_BITMAP, (void**)&cpu_bitmap, OPAL_STRING) ||  | 
 | 88 | +        NULL == cpu_bitmap || 0 == strlen(cpu_bitmap)) {  | 
 | 89 | +        /* we are not bound, so indicate that by setting OMP_PROC_BIND = false */  | 
 | 90 | +        opal_setenv("OMP_PROC_BIND", "0", true, environ_copy);  | 
 | 91 | +    } else {  | 
 | 92 | +        /* we are bound to something, so indicate that by setting OMP_PROC_BIND = true */  | 
 | 93 | +        opal_setenv("OMP_PROC_BIND", "1", true, environ_copy);  | 
 | 94 | +        /* compose OMP_PLACES to indicate where we are bound - sadly, the OMP folks  | 
 | 95 | +         * use a different syntax than HWLOC, an so we can't just provide the bitmap  | 
 | 96 | +         * string. So we will traverse the bitmap and convert as required */  | 
 | 97 | +        ranges = opal_argv_split(cpu_bitmap, ',');  | 
 | 98 | +        newrange = NULL;  | 
 | 99 | +        results = NULL;  | 
 | 100 | +        for (i=0; NULL != ranges[i]; i++) {  | 
 | 101 | +            if (NULL == (ptr = strchr(ranges[i], '-'))) {  | 
 | 102 | +                opal_argv_append_nosize(&newrange, ranges[i]);  | 
 | 103 | +            } else {  | 
 | 104 | +                /* terminate any existing range */  | 
 | 105 | +                if (NULL != newrange) {  | 
 | 106 | +                    param = opal_argv_join(newrange, ',');  | 
 | 107 | +                    asprintf(&tmp, "{%s}", param);  | 
 | 108 | +                    opal_argv_append_nosize(&results, tmp);  | 
 | 109 | +                    free(tmp);  | 
 | 110 | +                    free(param);  | 
 | 111 | +                    opal_argv_free(newrange);  | 
 | 112 | +                    newrange = NULL;  | 
 | 113 | +                }  | 
 | 114 | +                *ptr = '\0';  | 
 | 115 | +                ++ptr;  | 
 | 116 | +                start = strtol(ranges[i], NULL, 10);  | 
 | 117 | +                end = strtol(ptr, NULL, 10);  | 
 | 118 | +                asprintf(&tmp, "{%d:%d}", start, end - start + 1);  | 
 | 119 | +                opal_argv_append_nosize(&results, tmp);  | 
 | 120 | +                free(tmp);  | 
 | 121 | +            }  | 
 | 122 | +        }  | 
 | 123 | +        opal_argv_free(ranges);  | 
 | 124 | +        if (NULL != newrange) {  | 
 | 125 | +            param = opal_argv_join(newrange, ',');  | 
 | 126 | +            asprintf(&tmp, "{%s}", param);  | 
 | 127 | +            opal_argv_append_nosize(&results, tmp);  | 
 | 128 | +            free(tmp);  | 
 | 129 | +            free(param);  | 
 | 130 | +            opal_argv_free(newrange);  | 
 | 131 | +            newrange = NULL;  | 
 | 132 | +        }  | 
 | 133 | +        param = opal_argv_join(results, ',');  | 
 | 134 | +        opal_argv_free(results);  | 
 | 135 | +        opal_setenv("OMP_PLACES", param, true, environ_copy);  | 
 | 136 | +        free(param);  | 
 | 137 | +    }  | 
 | 138 | +    if (NULL != cpu_bitmap) {  | 
 | 139 | +        free(cpu_bitmap);  | 
 | 140 | +    }  | 
 | 141 | +}  | 
0 commit comments