优化矩阵乘法程序求bug解决方法

chenlei1700 2014-01-09 01:44:17
我照着资料伪代码写了个矩阵优化算法 思路就是充分利用1级缓存资源 把大矩阵分成若干个小矩阵进行乘算
问题是我算10*10的矩阵没有问题 算1000*1000就出bug了
我估计是内存分配或者指针越界的问题 但是我找不出来了
求高手帮我找找到底哪里出错了。
代码如下(ubuntu下编译的)


#include<sys/types.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <sys/wait.h>
#define N 100
#define THREAD_NUM 1

void thread_func(void *arg){
printf("thread start \n");
int i,j,k,i2,j2,k2;

double **a;
double **b;
double **c;
double *aa,*bb,*cc;


a = (double**)malloc(sizeof(int*)*N);
for (i=0; i<N; i++)
a[i] = (double*)malloc(sizeof(double)*N);

b = (double**)malloc(sizeof(double*)*N);
for (i=0; i<N; i++)
b[i] = (double*)malloc(sizeof(double)*N);

c = (double**)malloc(sizeof(double*)*N);
for (i=0; i<N; i++)
c[i] = (double*)malloc(sizeof(double)*N);



srand((unsigned)time(NULL));
for(i = 0; i < N; i++)
for(j = 0; j <N; j++)
*(*(a+i)+j) = 555.123;
for(i = 0; i < N; i++)
for(j = 0; j <N; j++){
*(*(b+i)+j) = 666.456;
}
for(i = 0; i < N; i++)
for(j = 0; j <N; j++){
*(*(c+i)+j) = 0.0;
if(j==N-1&&i==N-1)
printf("*(*(b+i)+j) =%f \n",*(*(b+i)+j));
}

for(i = 0; i < N; i+=8)
for(j = 0; j < N; j+=8)
for(k = 0; k < N; k+=8)
for(i2=0,cc=&(*(*(c+i)+j)),aa=&(*(*(a+i)+k));i2<8;++i2,cc+=N,aa+=N)
for(k2=0,bb=&(*(*(b+k)+j));k2<8;++k2,bb+=N)
for(j2=0;j2<8;++j2){
*(cc+j2)+=*(aa+k2)*(*(bb+j2));
//printf("cc ,*(cc+j2)%d %d\n",cc,*(cc+j2));

}


printf("ready to free \n");

for (i=0; i<N; i++)
free(a[i]); free(b[i]);free(c[i]);
free(a);free(b);free(c);
printf("thread done \n");

}




int main(void)
{
printf("program start \n");
int i;

pthread_t handle[THREAD_NUM];

for( i=0;i<THREAD_NUM;i++)
pthread_create(&handle[i],NULL,(void*)thread_func,NULL);

for(i=0;i<THREAD_NUM;i++)
pthread_join(handle[i],NULL);

perror("a*b leaveing");
return 0;
}
...全文
139 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-01-09
  • 打赏
  • 举报
回复
进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
赵4老师 2014-01-09
  • 打赏
  • 举报
回复
请判断malloc的返回值。
chenlei1700 2014-01-09
  • 打赏
  • 举报
回复
谢谢指正 但还是不行 program start thread start Segmentation fault (core dumped)
buyong 2014-01-09
  • 打赏
  • 举报
回复
a = (double**)malloc(sizeof(double*)*N);

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧