0

I am trying to measure the time it takes for a thread from creation to actually start.

Using POSIX thread on a Debian 6.0 machine with 32-cores (no hyper-threading) and calling pthread_attr_setaffinity_np function to set the affinity.

In a loop, I am creating the threads, waiting for them to finish, repeatedly.

So, my code looks like the following (thread 0 is running this).

for (ni=0; ni<n; ni++)
   {
      pthread_t *thrds;
      pthread_attr_t attr;
      cpu_set_t cpuset;
      ths = 1; // thread starts from 1

      thrds = malloc(sizeof(pthread_t)*nt); // thrds[0] not used
      assert(!pthread_attr_init(&attr));
      for (i=ths; i<nt; i++)
      {
         pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
         CPU_ZERO(&cpuset);
         CPU_SET(i, &cpuset); // setting i as the affinity for thread i
         assert(!pthread_attr_setaffinity_np(&attr,
                  sizeof(cpu_set_t), &cpuset));
         assert(!pthread_create(thrds+i, &attr, DoWork, i));
      }
      pthread_attr_destroy(&attr);

      DoWork(0);

      for (i=ths; i<nt; i++)
      {
         pthread_join(thrds[i], NULL);
      }
      if (thrds) free(thrds);
   }

Inside the thread function, I am calling sched_getcpu() to verify that the affinity is working. The problem is, this verification only passes the first iteration of i-loop. For the second iteration, thrd[1] gets the affinity of nt-1 (instead of 1) and so on.

Can anyone please explain why? And/or how to fix it?

NOTE: I found a workaround that if I put the master thread to sleep for 1 second after the join finishes at each iteration, the affinity works correctly. But this sleep duration could different on other machines. So still need a real fix for the issue.

Rakib
  • 791
  • 8
  • 19
  • `pthread_setaffinity_np` takes several arguments, such as the thread ID. You can't set the affinity of a thread before `pthread_create`, because it doesn't exist; you need its ID. – Kaz Jul 15 '14 at 22:11
  • My mistake. The function I am using actually is pthread_attr_setaffinity_np . Hope this clears up the confusion. – Rakib Jul 15 '14 at 22:20
  • You should post your actual code. The sample has issues, like missing function arguments. The two inner loops don't use exactly the same bounds, and seem to be wrongly using `thrd[i]`: the outer loop variable rather than the inner one, `thrd[t]`. – Kaz Jul 15 '14 at 22:48
  • Sorry for the typos. Just wanted to keep it simple. Now it should be correct. – Rakib Jul 17 '14 at 19:00

0 Answers0