如何使多线程应用程序使用VMWare下Ubuntu上的所有内核? [英] How do I make a multi-threaded app use all the cores on Ubuntu under VMWare?

查看:104
本文介绍了如何使多线程应用程序使用VMWare下Ubuntu上的所有内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多线程应用程序,可以处理非常大的数据文件.在Window 7上运行良好,代码全部为C ++,使用pthreads库进行跨平台多线程处理.当我在Windows上的Intel i3上运行它时-任务管理器显示所有四个核心都已达到极限,这正是我想要的.使用g ++ Ubuntu/VMWare工作站编译了相同的代码-启动了相同数量的线程,但是所有线程都在一个内核上运行(据我所知-任务管理器仅显示一个内核处于繁忙状态).

I have got a multi-threaded app that process a very large data file. Works great on Window 7, the code is all C++, uses the pthreads library for cross-platform multi-threading. When I run it under Windows on my Intel i3 - Task manager shows all four cores pegged to the limit, which is what I want. Compiled the same code using g++ Ubuntu/VMWare workstation - same number of threads are launched, but all threads are running on one core (as far as I can tell - Task Manager only shows one core busy).

我将深入研究pThreads调用-也许我错过了一些默认设置-但是如果有人有任何想法,我想听听他们的意见,我可以提供更多信息-

I'm going to dive into the pThreads calls - perhaps I missed some default setting - but if anybody has any idea, I'd like to hear them, and I can give more info -

更新:我确实设置了VMWare以查看所有四个内核,/proc/cpuinfo显示了四个内核

Update: I did setup VMWare to see all four cores and /proc/cpuinfo shows 4 cores

更新2 -刚刚编写了一个简单的应用程序来显示问题-也许仅是VMWare? -那里的任何Linux本地人士都想尝试看看这是否真正加载了多个内核?要在Windows上运行此程序,您将需要pThread库-可轻松下载.而且,如果有人可以建议比打印功能更密集的cpu,那就继续吧!

Update 2 - just wrote a simple app to show the problem - maybe it's VMWare only? - any Linux natives out there want to try and see if this actually loads down multiple cores? To run this on Windows you will need the pThread library - easily downloadable. And if anyone can suggest something more cpu intensive than printf- go ahead!

#ifdef _WIN32
#include "stdafx.h"
#endif
#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"

void *Process(void *data)
{
   long id = (long)data;
   for (int i=0;i<100000;i++)
   {
      printf("Process %ld says Hello World\n",id);
   }
   return NULL;
}

#ifdef _WIN32
int _tmain(int argc, _TCHAR* argv[])
#else
int main(int argc, char* argv[])
#endif
{
   int numCores = 1;
   if (argc>1)
      numCores = strtol(&argv[1][2],NULL,10);
   pthread_t *thread_ids = (pthread_t *)malloc(numCores*sizeof(pthread_t));
   for (int i=0;i<numCores;i++)
   {
      pthread_create(&thread_ids[i],NULL,Process,(void *)i);
   }
   for (int i=0;i<numCores;i++)
   {
      pthread_join(thread_ids[i],NULL);
   }
    return 0;
}

推荐答案

我对您的代码做了一些更改.我将numCores = strtol(&argv[1][2], NULL, 10);更改为numCores = strtol(&argv[1][0], NULL, 10);以通过调用./core 4使它在Linux下工作,也许您在内核数之前传递了某些内容,或者因为类型_TCHAR为每个字符3byte?对Windows并不那么熟悉.此外,由于我仅用printf就无法对CPU施加压力,因此我也对Process进行了一些更改.

I changed your code a bit. I changed numCores = strtol(&argv[1][2], NULL, 10); to numCores = strtol(&argv[1][0], NULL, 10); to make it work under Linux by calling ./core 4 maybe you where passing something in front of the number of cores, or because type _TCHAR is 3byte per char? Not that familiar with windows.. Further more since I wasn't able to stress the CPU with only printf I also changed Process a bit.

void *Process(void *data)
{
     long hdata = (long)data;
     long id = (long)data;
     for (int i=0;i<10000000;i++)
     {
         printf("Process %ld says Hello World\n",id);
         for (int j = 0; j < 100000; j++)
         {
               hdata *= j;
               hdata *= j;
               hdata *= j;
               hdata *= j;
               hdata *= j;
               hdata *= j;
               hdata *= j;
               hdata *= j;
               hdata *= j;
               hdata *= j;
               hdata *= j;
               ...
         }
    }

    return (void*)hdata;
}

现在,当我运行gcc -O2 -lpthread -std=gnu99 core.c -o core && ./core 4时,您可以看到所有4个线程都很好地在4个不同的内核上运行,一次可能在一个内核之间交换,但是所有4个内核都在超时工作.

And now when I run gcc -O2 -lpthread -std=gnu99 core.c -o core && ./core 4 You can see that all 4 threads are running on 4 different cores well probably the are swapped from core to core at a time but all 4 cores are working overtime.

core.c: In function ‘main’:
core.c:75:50: warning: cast to pointer from integer of different size [-Wint-to-pointer-   cast]
Starting 4 threads
Process 0 says Hello World
Process 1 says Hello World
Process 3 says Hello World
Process 2 says Hello World

我用htop进行了验证,希望对您有所帮助.:)我运行带有4cores的专用Debian SID x86_64,以防您感到奇怪.

I verified it with htop hope it helps.. :) I run dedicated Debian SID x86_64 with 4cores in case you're wondering.

这篇关于如何使多线程应用程序使用VMWare下Ubuntu上的所有内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆