MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
task2cpu.cc File Reference
#include "mbconfig.h"
#include "ac/pthread.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include "myassert.h"
Include dependency graph for task2cpu.cc:

Go to the source code of this file.

Functions

int mbdyn_task2cpu (int cpu)
 

Variables

static bool mbdyn_task2cpu_disabled = false
 

Function Documentation

int mbdyn_task2cpu ( int  cpu)

Definition at line 56 of file task2cpu.cc.

References DEBUGCERR, and mbdyn_task2cpu_disabled.

57 {
58  int fd = -1;
59 
60 #ifdef HAVE_THREADS
61  pthread_mutex_lock(&::mbdyn_task2cpu_mutex);
62 #endif /* HAVE_THREADS */
63  if (!::mbdyn_task2cpu_disabled) {
64 #ifdef HAVE_TASK2CPU
65  fd = open("/dev/TASK2CPU", O_RDWR);
66  if (fd != -1) {
67  ioctl(fd, 0, cpu);
68  close(fd);
69 
70  } else {
71  int save_errno = errno;
72  char *err_msg = strerror(save_errno);
73 
74  silent_cerr("Error opening /dev/TASK2CPU ("
75  << save_errno << ": " << err_msg << ";"
76  " ignored)" << std::endl);
78  }
79 #elif defined(USE_PTHREAD_SETAFFINITY_NP)
80  cpu_set_t cpuset;
81  CPU_ZERO(&cpuset);
82  CPU_SET(cpu, &cpuset);
83 
84  const pthread_t thread = pthread_self();
85 
86  int s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
87 
88  if (0 != s) {
89  silent_cerr("warning: pthread_setaffinity_np failed with status " << s << std::endl);
90  goto failed;
91  }
92 
93 #ifdef DEBUG
94  s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset);
95 
96  if (0 != s) {
97  DEBUGCERR("warning: pthread_getaffinity_np failed with status " << s << std::endl);
98  goto failed;
99  }
100 
101  for (int i = 0; i < CPU_SETSIZE; ++i) {
102  if (CPU_ISSET(i, &cpuset)) {
103  DEBUGCERR("thread " << cpu + 1 << " running at CPU " << i << std::endl);
104  }
105  }
106 #endif
107  fd = 1; // return 0
108 
109  failed:
110  if (fd == -1) {
112  }
113 #else /* ! HAVE_TASK2CPU */
114  silent_cerr("/dev/TASK2CPU or pthread_getaffinity_np are not available" << std::endl);
116 #endif /* ! HAVE_TASK2CPU */
117  }
118 #ifdef HAVE_THREADS
119  pthread_mutex_unlock(&::mbdyn_task2cpu_mutex);
120 #endif /* HAVE_THREADS */
121 
122  return (fd == -1);
123 }
#define DEBUGCERR(msg)
Definition: myassert.h:235
static bool mbdyn_task2cpu_disabled
Definition: task2cpu.cc:50

Variable Documentation

bool mbdyn_task2cpu_disabled = false
static

Definition at line 50 of file task2cpu.cc.

Referenced by mbdyn_task2cpu().