如何将线程分配给C中的不同内核? [英] How to assign threads to different cores in C?

查看:47
本文介绍了如何将线程分配给C中的不同内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,该程序使用 4 个线程将 8 个数字相加,然后计算结果的乘积.如何确保每个线程都使用单独的内核以获得最大的性能提升.我是 pthreads 的新手,所以我真的不知道如何正确使用它.请提供尽可能简单的答案.

I created a program that does the addition of 8 numbers using 4 threads, and then the product of the results. How to ensure that each thread is using a separate core for maximum performance gains. I am new to pthreads so I really don't have any idea on how to use it properly. Please provide answers as simple as possible.

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int global[9];
void *sum_thread(void *arg)
{
    int *args_array;
    args_array = arg;
    int n1,n2,sum;
    n1=args_array[0];
    n2=args_array[1];
    sum = n1*n2;

    printf("N1 * N2 = %d\n",sum);
    return (void*) sum;
}
void *sum_thread1(void *arg)
{
    int *args_array;
    args_array = arg;
    int n3,n4,sum2;
    n3=args_array[2];
    n4=args_array[3];
    sum2=n3*n4;
    printf("N3 * N4 = %d\n",sum2);
    return (void*) sum2;
}
void *sum_thread2(void *arg)
{
    int *args_array;
    args_array = arg;
    int n5,n6,sum3;
    n5=args_array[4];
    n6=args_array[5];
    sum3=n5*n6;
    printf("N5 * N6 = %d\n",sum3);
    return (void*) sum3;
}
void *sum_thread3(void *arg)
{
    int *args_array;
    args_array = arg;
    int n8,n7,sum4;
    n7=args_array[6];
    n8=args_array[7];
    sum4=n7*n8;
    printf("N7 * N8 = %d\n",sum4);
    return (void*) sum4;
}
int main()
{
    int sum3,sum2,sum,sum4;
    int prod;
    global[0]=9220; global[1]=1110; global[2]=1120; global[3]=2320; global[4]=5100; global[5]=6720; global[6]=7800; global[7]=9290;// the input
    pthread_t tid_sum;
    pthread_create(&tid_sum,NULL,sum_thread,global);
    pthread_join(tid_sum,(void*)&sum);
    pthread_t tid_sum1;
    pthread_create(&tid_sum1,NULL,sum_thread1,global);
    pthread_join(tid_sum1,(void*)&sum2);
    pthread_t tid_sum2;
    pthread_create(&tid_sum2,NULL,sum_thread2,global);
    pthread_join(tid_sum2,(void*)&sum3);
    pthread_t tid_sum3;
    pthread_create(&tid_sum3,NULL,sum_thread3,global);
    pthread_join(tid_sum3,(void*)&sum4);
    prod=sum+sum2+sum3+sum4;
    printf("The sum of the products is: %d", prod);
    return 0;
}

推荐答案

你没有、不想也不能(我不知道你是否能以某种方式)管理如此低的硬件资源水平.这是您的操作系统的工作,部分是标准库的工作:它们已经过适当的优化和标准化测试.

You don't have, don't want and mustn't (I don't know if you somehow you can though) manage hardware resources at such low levels. That's a job for your OS and partially for standard libraries: they have been tested optimized and standardized properly.

我怀疑你能做得更好.如果你按照你说的去做,要么你是一名专业的硬件/操作系统程序员,要么你正在破坏几十年的工作:).

I doubt you can do better. If you do what you are saying either you are an expert hardware/OS programmer or you are destroying decades of works :) .

还要考虑这个事实:如果您可以手动索引内核,那么您的代码将不再具有可移植性,因为这取决于您机器的内核数量.

Also consider this fact: your code will not be portable anymore if you could index the cores manually since it depends on the number of cores of your machine.

另一方面,即使只有一个内核,多线程程序也应该可以工作(有时甚至更好).一个例子是一个线程在事件发生之前不做任何事情的情况:您可以让一个线程进入睡眠"状态,以便只有其他线程使用 CPU;然后当事件发生时它会执行.在非多线程程序中,通常使用轮询,它使用 CPU 资源什么也不做.

On the other way multithread programs should work (and even better sometimes) even when having one core. An example is the case where one of the threads doesn't do anything until an event happens: you can make one thread go to "sleep" so that only the other threads use the CPU; then when the event happens it will execute. In a non-multithread program generally polling is used which uses CPU resource to do nothing.

另外@yano 说你是多线程程序在这种情况下并不是真正的并行,因为你正在创建线程,然后在启动其他线程之前等待它用 pthread_join 完成.

Also @yano said you are multithread program is not really parallel in this case since you are creating the thread and then waiting for it to finish with pthread_join before starting the other threads.

这篇关于如何将线程分配给C中的不同内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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