1
我得到一個分段錯誤(核心轉儲)錯誤,當我試圖乘以兩個矩陣線程 我做了這個程序在C中,並與TAM < = 200正常工作,但是當我插入高價值的線程不起作用。 這是代碼:線程的分段錯誤(核心轉儲)
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "tamanio.h"
#define NUM_THREADS 4
int matrizA[TAM][TAM];
int matrizB[TAM][TAM];
int matrizR[TAM][TAM];
int x,y,z;
int m=TAM,o=TAM,p=TAM;
clock_t start_t, end_t;
double total_t;
//THREADS
void *mul(void *threadid){
long tid;
tid = (long) threadid;
int OTHERCONTROL=tid*TAM/NUM_THREADS;
int VARCONTROLTHREAD=(tid+1)*TAM/NUM_THREADS;
printf("Iniciando hilo %ld ...\n", tid);
//multiply
for(x=OTHERCONTROL; x<VARCONTROLTHREAD; x++){
for(y=0; y<o; y++){
for(z=0;z<p;z++){
matrizR[x][y]+=matrizA[x][z]*matrizB[z][y];
}
}
}
printf("Hilo %ld terminado.\n", tid);
pthread_exit((void*) threadid);}
int main (int argc, char **argv){
//variables
FILE *entrada;
char *nombre;
//Read
if (argc > 1){
nombre =argv[1];
entrada =fopen(nombre, "r");
if(entrada == NULL){
printf("Hubo problemas al intentar abrir el archivo de entrada\n");
exit(1);
}
}
// MatrizA
for(x=0; x<m; x++){
for(y=0; y<o; y++){
fscanf(entrada,"%d\t",&(matrizA[x][y]));
}
}
// MatrizB
for(x=0; x<m; x++){
for(y=0; y<o; y++){
fscanf(entrada,"%d\t",&(matrizB[x][y]));
}
}
fclose(entrada);
printf("Se termina la lectura de datos\n");
start_t=clock();
//**THREADS**
pthread_t threads[NUM_THREADS];
int rc;
long t;
void *status;
for(t=0;t<NUM_THREADS;t++){
printf("creando hilo%ld\n",t);
rc=pthread_create(&threads[t],NULL,mul,(void *)t);
if(rc){
printf("ERROR\n");
exit(-1);
}
}
for(t=0; t<NUM_THREADS; t++) {
rc = pthread_join(threads[t], &status);
if (rc) {
printf("ERROR; El codigo de retorno de pthread_join() es %d\n", rc);
exit(-1);
}
printf("Main: se completo la union con el hilo %ld regresando un estado de %ld\n",t,(long)status);
}
end_t=clock();
total_t = (double) (end_t - start_t)/CLOCKS_PER_SEC;
printf("Tiempo de ejecucion: %f \n",total_t);
printf("END MAIN\n");
pthread_exit(NULL);
return 0;
}
tamanio.h只含有一種被稱爲TAM
#define TAM 1000
變量是什麼問題?
[JS1](http://stackoverflow.com/users/4192931/js1)的[回覆](http://stackoverflow.com/a/27415229/15168)提出了一些有效的觀點。我在我的Mac上進行了一些測試,其中'TAM'設置爲'300','NUM_THREADS'設爲'25',文件爲18,000個隨機單位數字(1..9),變量'x' ,'y'和'z'做成局部變量,它對我來說似乎很好。 – 2014-12-11 04:49:44