I wrote a program that uses multithreading for parallel computing. I checked that on my system (OS X) it simultaneously allocates both cores. I just ported it to Ubuntu without any changes, because I encoded it based on this platform. In particular, I run the Canonical OneVic HVM image on Amazon EC2, the cluster computes a 4x large instance. These machines are equipped with 2 Intel Xeon X5570 processors, quad-core processors.
Unfortunately, my program does not perform multithreading on an EC2 machine. Performing more than 1 thread actually slows down the calculation for each additional thread. When I start my program, it is shown that when initializing more than 1 thread, the system% of CPU consumption is approximately proportional to the number of threads. Only 1 stream,% sy ~ 0.1. In any case, the user% never exceeds ~ 9%.
Below are sections related to my code stream
const int NUM_THREADS = N; //where changing N is how I set the # of threads void Threading::Setup_Threading() { sem_unlink("producer_gate"); sem_unlink("consumer_gate"); producer_gate = sem_open("producer_gate", O_CREAT, 0700, 0); consumer_gate = sem_open("consumer_gate", O_CREAT, 0700, 0); completed = 0; queued = 0; pthread_attr_init (&attr); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); } void Threading::Init_Threads(vector <NetClass> * p_Pop) { thread_list.assign(NUM_THREADS, pthread_t()); for(int q=0; q<NUM_THREADS; q++) pthread_create(&thread_list[q], &attr, Consumer, (void*) p_Pop ); } void* Consumer(void* argument) { std::vector <NetClass>* p_v_Pop = (std::vector <NetClass>*) argument ; while(1) { sem_wait(consumer_gate); pthread_mutex_lock (&access_queued); int index = queued; queued--; pthread_mutex_unlock (&access_queued); Run_Gen( (*p_v_Pop)[index-1] ); completed--; if(!completed) sem_post(producer_gate); } } main() { ... t1 = time(NULL); threads.Init_Threads(p_Pop_m); for(int w = 0; w < MONTC_NUM_TRIALS ; w++) { queued = MONTC_POP; completed = MONTC_POP; for(int q = MONTC_POP-1 ; q > -1; q--) sem_post(consumer_gate); sem_wait(producer_gate); } threads.Close_Threads(); t2 = time(NULL); cout << difftime(t2, t1); ... }
source share