gpt4 book ai didi

c - 测量多线程 C 程序的加速(使用 Pthreads 实现)

转载 作者:行者123 更新时间:2023-12-04 20:53:26 25 4
gpt4 key购买 nike

我目前有一个使用 Pthreads 编码的多线程 C 程序,它使用 2 个线程。我想增加编号。的线程和测量速度这样做。我想以自动方式运行我的代码,其中没有。使用的线程数不断增加,我想以图形方式显示代码的运行时间。如果我能获得有关如何执行此操作的线索,尤其是有关如何自动化整个过程并以图形方式绘制的线索,我会很高兴。这是我的代码:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 2
#define VECTOR_SIZE 40

struct DOTdata
{
/* data */
long X[VECTOR_SIZE];
long Y[VECTOR_SIZE];
long sum;
long compute_length;
};

struct DOTdata dotstr;
pthread_mutex_t mutex_sum;

void *calcDOT(void *);

int main(int argc, char *argv[])
{
long vec_index;

for(vec_index = 0 ; vec_index < VECTOR_SIZE ; vec_index++){
dotstr.X[vec_index] = vec_index + 1;
dotstr.Y[vec_index] = vec_index + 2;
}

dotstr.sum = 0;
dotstr.compute_length = VECTOR_SIZE/NUM_THREADS;

pthread_t call_thread[NUM_THREADS];
pthread_attr_t attr;
void *status;

pthread_mutex_init(&mutex_sum, NULL);

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

long i;

for(i = 0 ; i < NUM_THREADS ; i++){
pthread_create(&call_thread[i], &attr, calcDOT, (void *)i);
}

pthread_attr_destroy(&attr);

for (i = 0 ; i < NUM_THREADS ; i++){
pthread_join(call_thread[i], &status);
}

printf("Resultant X*Y is %ld\n", dotstr.sum);
pthread_mutex_destroy(&mutex_sum);
pthread_exit(NULL);
}

void *calcDOT(void *thread_id)
{
long vec_index;
long start_index;
long end_index;
long length;
long offset;
long sum = 0;

offset = (long)thread_id;
length = dotstr.compute_length;

start_index = offset * length;
end_index = (start_index + length) - 1;

for(vec_index = start_index ; vec_index < end_index ; vec_index++){
sum += (dotstr.X[vec_index] * dotstr.Y[vec_index]);
}

pthread_mutex_lock(&mutex_sum);
dotstr.sum += sum;
pthread_mutex_unlock(&mutex_sum);

pthread_exit((void *)thread_id);

}

我想递增我的 NUM_THREADS 参数并在每次递增后运行它,记录每次递增后的执行时间并绘制执行时间与线程数的关系图。

最佳答案

我尝试了一种简单的方法,即增加线程数,使用 time.h 对其进行计时并使用 gnuplot 对其进行绘图。每次迭代我们都会将线程数加倍,并打印一次迭代的时间。我们使用 gnuplot 显示一个图表,x 轴为线程数,y 轴为执行时间

enter image description here

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM_THREADS 2
#define VECTOR_SIZE 40

struct DOTdata {
/* data */
long X[VECTOR_SIZE];
long Y[VECTOR_SIZE];
long sum;
long compute_length;
};

struct DOTdata dotstr;
pthread_mutex_t mutex_sum;

void *calcDOT(void *);

int main(int argc, char *argv[]) {
double xvals[VECTOR_SIZE / NUM_THREADS];
double yvals[VECTOR_SIZE / NUM_THREADS];
int index = 0;
for (int count = NUM_THREADS; count < VECTOR_SIZE / NUM_THREADS; count = count * 2) {

clock_t begin = clock();

long vec_index;

for (vec_index = 0; vec_index < VECTOR_SIZE; vec_index++) {
dotstr.X[vec_index] = vec_index + 1;
dotstr.Y[vec_index] = vec_index + 2;
}

dotstr.sum = 0;
dotstr.compute_length = VECTOR_SIZE / count;

pthread_t call_thread[count];
pthread_attr_t attr;
void *status;

pthread_mutex_init(&mutex_sum, NULL);

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

long i;

for (i = 0; i < count; i++) {
pthread_create(&call_thread[i], &attr, calcDOT, (void *) i);
}

pthread_attr_destroy(&attr);

for (i = 0; i < count; i++) {
pthread_join(call_thread[i], &status);
}

printf("Resultant X*Y is %ld\n", dotstr.sum);
pthread_mutex_destroy(&mutex_sum);
clock_t end = clock();
double time_spent = (double) (end - begin) / CLOCKS_PER_SEC;

printf("time spent: %f NUM_THREADS: %d\n", time_spent, count);
xvals[index] = count;
yvals[index] = time_spent;
index++;
}

FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");

fprintf(gnuplotPipe, "plot '-' \n");

for (int i = 0; i < VECTOR_SIZE / NUM_THREADS; i++)
{
fprintf(gnuplotPipe, "%lf %lf\n", xvals[i], yvals[i]);
}

fprintf(gnuplotPipe, "e");


pthread_exit(NULL);
}

void *calcDOT(void *thread_id) {
long vec_index;
long start_index;
long end_index;
long length;
long offset;
long sum = 0;

offset = (long) thread_id;
length = dotstr.compute_length;

start_index = offset * length;
end_index = (start_index + length) - 1;

for (vec_index = start_index; vec_index < end_index; vec_index++) {
sum += (dotstr.X[vec_index] * dotstr.Y[vec_index]);
}

pthread_mutex_lock(&mutex_sum);
dotstr.sum += sum;
pthread_mutex_unlock(&mutex_sum);

pthread_exit((void *) thread_id);

}

输出

Resultant X*Y is 20900
time spent: 0.000155 NUM_THREADS: 2
Resultant X*Y is 19860
time spent: 0.000406 NUM_THREADS: 4
Resultant X*Y is 17680
time spent: 0.000112 NUM_THREADS: 8
Resultant X*Y is 5712
time spent: 0.000587 NUM_THREADS: 16

关于c - 测量多线程 C 程序的加速(使用 Pthreads 实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41415541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com