2017-07-21 9 views
1

私はそれがこの種のものになると、noobです。shed_getaffinity、cpu_set_tなどの代わりに?

私はMacOSで気候モデルをコンパイルするために苦労していると私はここで何が起こっているかにそれを煮詰めてきました:

#define _GNU_SOURCE 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include <sched.h> 
#include <errno.h> 
#include <sys/resource.h> 
#include <sys/syscall.h> 

static pid_t gettid(void) 
{ 
    return syscall(__NR_gettid); 
} 

/*                                                         
* Returns this thread's CPU affinity, if bound to a single core,                                         
* or else -1.   




*/ 
int get_cpu_affinity(void) 
{ 
    cpu_set_t coremask;   /* core affinity mask */ 

    CPU_ZERO(&coremask); 
    if (sched_getaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) { 
    fprintf(stderr,"Unable to get thread %d affinity. 

%s\n",gettid(),strerror(errno)); 
    } 

    int cpu; 
    int first_cpu = -1; /* first CPU in range */ 
    int last_cpu = -1; /* last CPU in range */ 
    for (cpu=0;cpu < CPU_SETSIZE;cpu++) { 
    if (CPU_ISSET(cpu,&coremask)) { 
     if (first_cpu == -1) { 
     first_cpu = cpu; 
     } else { 
     last_cpu = cpu; 
     } 
    } 
    } 

    return (last_cpu == -1) ? first_cpu : -1; 

} 

int get_cpu_affinity_(void) { return get_cpu_affinity(); }  /* Fortran interface */ 


/*                                                         
* Set CPU affinity to one core.                                                 
*/ 

void set_cpu_affinity(int cpu) 
{ 
    cpu_set_t coremask;   /* core affinity mask */ 

    CPU_ZERO(&coremask); 
    CPU_SET(cpu,&coremask); 
    if (sched_setaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) { 
    fprintf(stderr,"Unable to set thread %d affinity. %s\n",gettid(),strerror(errno)); 

    } 
} 

void set_cpu_affinity_(int *cpu) { set_cpu_affinity(*cpu); } /* Fortran interface */ 

コンパイルすると、私はエラーのカップルを取得:

ファースト - 識別子 "cpu_set_t"は定義されていません。第2識別子 "CPU_SETSIZE"は定義されていません。

私はグーグルをしていますが、sched_getaffinitiy()、cpu_set_tなどがあります。

Cプログラミングのすべての種類の私は本当に私の深さの外です。私はmacOSのためにこれをやっている代わりの方法を知っている人がいて、それをどうやってやることができるのだろうかと思っていました。

以下の完全なエラーレポートを添付しました。

種類よろしく、

ニール:)

完全なエラーレポート:

(python2) salvare:MASTERS Neil$ python run.py 
Working directory for exp 'playground' already exists 
RRTM compilation disabled. Namelist set to gray radiation. 
Writing path_names to '/Users/Neil/MASTERS/ISCA_TEMP/playground/path_names' 
Running compiler 
/Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh: line 21: module: command not found 
loadmodules 
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 3: module: command not found 
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 4: module: command not found 
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 5: module: command not found 
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 6: module: command not found 
/Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh: line 23: module: command not found 
/Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh: line 24: ulimit: stack size: cannot modify limit: Operation not permitted 
./compile_mppn.sh: line 13: module: command not found 
./compile_mppn.sh: line 14: module: command not found 
./compile_mppn.sh: line 15: module: command not found 
- mppnccombine.c:162:24: warning: format string is not a string literal (potentially insecure) [-Wformat-security] 
sprintf(outfilename,argv[outputarg]); outlen=strlen(outfilename); 
^~~~~~~~~~~~~~~ 
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf' 
__builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__) 
^~~~~~~~~~~ 
mppnccombine.c:162:24: note: treat the string as an argument to avoid this 
sprintf(outfilename,argv[outputarg]); outlen=strlen(outfilename); 
^ 
"%s", 
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf' 
__builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__) 
^ 
1 warning generated. 
ln: /Users/Neil/MASTERS/ISCA_TEMP/playground/exec/mppnccombine.x: File exists 
Makefile is ready. 
mpicc -Duse_libMPI -Duse_netCDF -Duse_LARGEFILE -DINTERNAL_FILE_NML -DOVERLOAD_C8 -DRRTM_NO_COMPILE -I/usr/local/include -D__IFC -c /Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c 
......................................................................................................................../Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(35): error: identifier "__NR_gettid" is undefined 
eturn syscall(__NR_gettid); 
^ 
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(44): error: identifier "cpu_set_t" is undefined 
cpu_set_t coremask;  /* core affinity mask */ 
^ 
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(47): error: identifier "cpu_set_t" is undefined 
if (sched_getaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) { 
^ 
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(54): error: identifier "CPU_SETSIZE" is undefined 
for (cpu=0;cpu < CPU_SETSIZE;cpu++) { 
^ 
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(75): error: identifier "cpu_set_t" is undefined 
cpu_set_t coremask;  /* core affinity mask */ 
^ 
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(79): error: identifier "cpu_set_t" is undefined 
if (sched_setaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) { 
^ 
compilation aborted for /Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c (code 2) 
make: *** [affinity.o] Error 2 
ERROR: mkmf failed for fms_moist 
CRITICAL - Compilation failed. 
Traceback (most recent call last): 
    File "run.py", line 25, in <module> 
    exp.compile() 
    File "/Users/Neil/MASTERS/Isca/src/extra/python/gfdl/experiment.py", line 298, in compile 
    raise e 
sh.ErrorReturnCode_1: 

    RAN: /bin/bash /Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh 

    STDOUT: 


    STDERR: 

答えて

0

あなたは正しい軌道に乗ってあなたを置くグーグル - sched_getaffinity()とそれが使用するcpu_set_t構造は、Linux固有のものです、すべてのプラットフォームで利用できるわけではありません。特にMac OSXはそれらを見失っているようだ。

しかし、代替手段があります。 a blog post from someone porting code to OSXが見つかりました。代わりにsysctlbyname()を使用しています。そのサイトのコードは、machdep.cpu.core_countを要求し、それを使ってcpu_set_tという独自のバージョンを構築することによって、Mac OSX上でsched_getaffinity()を再実装します。

+0

ありがとうございます。私の知識は非常に基本的なものなので、再び簡単な質問です。いったん私は自分のバージョンのcpu_set_tを「作成」(コピー)したら、どうすれば使用できますか?私はどのようにして上記のスクリプトがそれにアクセスできるようにするのですか?ありがとう。 –

+0

@ NeeLewis、これは明らかにするために、これはLinuxアフィニティAPIの「作成者のための作業」実装です。 macOSアフィニティヒントは単なるヒントです。他のほとんどのオペレーティングシステム(Windowsの場合でも)が提供するハードスレッド/プロセスのバインド/ピン割り当てと同等の機能はありません。これは、いくつかの高性能アプリケーションの大きな欠点です。私は、スレッドの移行がLinux上で実行時間を50%以上増加させるケースを見てきました。 –