我正在尝试使用多线程将两个矩阵相乘。在这里,我在Linux中使用gcc编译程序,并通过输入线程数来运行。
gcc multiThread.c -o test -lpthread
./test 4
在这里,我对N * N的矩阵进行乘法运算,其中N从10到1000,间隔为10,并计算每次迭代的执行时间。当我运行程序时,它给出了分段错误。请帮忙。
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include<time.h>
int SIZE = 10; // Size by SIZE matrices
int num_thrd; // number of threads
int A[2000][2000], B[2000][2000], C[2000][2000];
// initialize a matrix
void init_matrix(int m[SIZE][SIZE])
{
int i, j;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
m[i][j] = rand() % 100 + 1;
}
// thread function: taking "slice" as its argument
void* multiply(void* slice)
{
int s = (int)slice; // retrive the slice info
int from = (s * SIZE)/num_thrd; // note that this 'slicing' works fine
int to = ((s+1) * SIZE)/num_thrd; // even if SIZE is not divisible by num_thrd
int i,j,k;
printf("computing slice %d (from row %d to %d)\n", s, from, to-1);
for (i = from; i < to; i++)
{
for (j = 0; j < SIZE; j++)
{
C[i][j] = 0;
for ( k = 0; k < SIZE; k++)
C[i][j] += A[i][k]*B[k][j];
}
}
printf("finished slice %d\n", s);
}
int main(int argc, char* argv[])
{
FILE *outFile;
outFile = fopen("Algorithm3_Times.txt", "r");
pthread_t* thread; // pointer to a group of threads
for(int ini=0; ini<100; ini++)
{
int i;
if (argc!=2)
{
printf("Usage: %s number_of_threads\n",argv[0]);
exit(-1);
}
num_thrd = atoi(argv[1]);
init_matrix(A);
init_matrix(B);
clock_t start = clock();
thread = (pthread_t*) malloc(num_thrd*sizeof(pthread_t));
// this for loop not entered if threadd number is specified as 1
for (i = 1; i < num_thrd; i++)
{
// creates each thread working on its own slice of i
if (pthread_create (&thread[i], NULL, multiply, (void*)i) != 0 )
{
perror("Can't create thread");
free(thread);
exit(-1);
}
}
// main thread works on slice 0
// so everybody is busy
// main thread does everything if threadd number is specified as 1
multiply(0);
// main thead waiting for other thread to complete
for (i = 1; i < num_thrd; i++)
pthread_join (thread[i], NULL);
clock_t end = clock();
float time = (end - start)*1000 / CLOCKS_PER_SEC;
fprintf(outFile,"time taken for Multiplication using %d", num_thrd);
fprintf(outFile," threads = %f", time);
fprintf(outFile," milliseconds \n");
if (thread != NULL)
{
free(thread);
thread = NULL;
}
SIZE += 10;
}
printf("calculation completed.\n\n");
return 0;
}
最佳答案
C是一种硬语言,特别是因为默认情况下运行时错误不包含有用的调试信息。这是调试器进入的地方。
我如何使用docker / alpine调试它:
将代码放入~/gcc/t.c
以共享给我的dockerdocker run --rm -it -v
ls -d〜/gcc:/code alpine
apk add build-base gdb musl-dbg
安装gcc和朋友,gdb和musl-dbg,我需要在标准库中调试segfault。cd /code
gcc -g t.c -o t
gdb t
现在这是我的调试器会话:
GNU gdb (GDB) 8.0.1
[ ... preamble removed for brevity ... ]
Reading symbols from t...done.
(gdb) run 1
Starting program: /code/t 1
warning: Error disabling address space randomization: Operation not permitted
computing slice 0 (from row 0 to 9)
finished slice 0
Program received signal SIGSEGV, Segmentation fault.
vfprintf (f=0x0, fmt=0x560896553020 "time taken for Multiplication using %d",
ap=ap@entry=0x7ffcb18b29f8) at src/stdio/vfprintf.c:671
671 src/stdio/vfprintf.c: No such file or directory.
(gdb) bt
#0 vfprintf (f=0x0,
fmt=0x560896553020 "time taken for Multiplication using %d",
ap=ap@entry=0x7ffcb18b29f8) at src/stdio/vfprintf.c:671
#1 0x00007fa6ef0c056f in fprintf (f=<optimized out>, fmt=<optimized out>)
at src/stdio/fprintf.c:9
#2 0x0000560896552ee2 in main (argc=2, argv=0x7ffcb18b2b68) at t.c:87
太好了,现在我们要查看行号。第87行是这一行:
fprintf(outFile,"time taken for Multiplication using %d", num_thrd);
我们可以看一下这一行,做一点思考,最后看一下
outFile
的定义:outFile = fopen("Algorithm3_Times.txt", "r");
啊哈!我们希望写入outFile,但我们将其打开以进行读取!我改写写作:
outFile = fopen("Algorithm3_Times.txt", "w");
和您的程序运行(请注意,下面我仅显示前10行)
/code # ./t 10 |head
computing slice 1 (from row 1 to 1)
finished slice 1
computing slice 2 (from row 2 to 2)
computing slice 3 (from row 3 to 3)
finished slice 3
finished slice 2
computing slice 4 (from row 4 to 4)
finished slice 4
computing slice 5 (from row 5 to 5)
finished slice 5
既然已经介绍了
gdb
,就可以开始在特定行中放入gdb break
语句,并解决所有剩余的错误。我的程序似乎还没有完成,但是我没有花太多时间。关于c - C语言中的多线程错误分割错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58881180/