#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>
#define N 2048
int **a;
int **b;
int **c;
int row_block;
void *cal_matrix(void *arg);
int main(int argc, char* argv[])
{
FILE *fp1, *fp2, *fp3;
time_t start, end;
int i, j;
// thread 갯수 int 형으로...
int thread_i = atoi(argv[4]);
// thread ID 부여받기 위한 변수
pthread_t *tcb = (pthread_t *)calloc(thread_i, sizeof(pthread_t));
// thread 에 argument 넘겨주기 위함
int *arg = (int *)calloc(thread_i, sizeof(int));
//하나의 thread 에 실행될 row 갯수
row_block = N / thread_i;
/*****************************/
/* argument 검사 */
/*****************************/
if (argc != 5)
{
printf("Argument is not enough! \n");
exit(1);
}
/*****************************/
/* FILE 열기 */
/*****************************/
fp1 = fopen(argv[1], "w+");
fp2 = fopen(argv[2], "w+");
fp3 = fopen(argv[3], "w+");
/*****************************/
/* 배열 동적 할당 */
/*****************************/
a = (int **)calloc(N, sizeof(int));
b = (int **)calloc(N, sizeof(int));
c = (int **)calloc(N, sizeof(int));
for (i = 0; i < N; i++)
{
a[i] = (int *)calloc(N, sizeof(int));
b[i] = (int *)calloc(N, sizeof(int));
c[i] = (int *)calloc(N, sizeof(int));
}
/*****************************/
/* 배열에 랜덤값 입력 */
/*****************************/
srand(time(NULL));
for (i = 0; i < N; i++ )
{
for (j = 0; j < N ; j++ )
{
a[i][j] = rand() % 100;
fprintf(fp1, "%d ", a[i][j]);
b[i][j] = rand() % 100;
fprintf(fp2, "%d ", b[i][j]);
}
}
/*****************************/
/* time 측정(start) */
/*****************************/
printf("program start ... \n");
start = time(NULL);
/*****************************/
/* pthread 생성 */
/*****************************/
for (i = 0; i < atoi(argv[4]); i++)
{
arg[i] = i;
pthread_create(tcb+i, NULL, cal_matrix, arg+i);
}
/*****************************/
/* thread 대기 */
/*****************************/
for (j = 0; j < atoi(argv[4]); j++)
{
pthread_join(tcb[i], NULL);
}
/*****************************/
/* 행렬 C 파일에 쓰기 */
/*****************************/
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
fprintf(fp3 , "%d ", c[i][j]);
}
}
/*****************************/
/* time 측정(end) */
/*****************************/
end = time(NULL);
printf("time : %d seconds\n", (end-start));
printf("c[0][0] : %d\n", c[0][0]);
printf("program end ... \n");
/*****************************/
/* 동적공간 반환 & FILE 닫음 */
/*****************************/
free(a);
free(b);
free(c);
free(tcb);
free(arg);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}// main
/*****************************/
/* 배열 곱셈하는 함수 */
/*****************************/
void *cal_matrix(void *arg)
{
int i, j, k;
int row = row_block * (*((int *)arg));
int box = row + row_block;
printf("row=%d row_block=%d\n", row, row_block);
for(i = 0; i < N ; i++)
{
for (j = 0; j < N; j++)
{
for (k = row; k < box; k++)
{
c[i][j] += a[i][k] * b[k][j];
}//for
}// for
}//for
}// cal_matrix
####### 로직은 맞는데 c 에 값이 제대로 안들어감 (누가 고쳐주셈.ㅡㅜ) #######
[보고서 다운로드]
[다른 완성본 다운로드]



나의 너의 친구는 위치의 현재 팬이 되었다!
우수한 위치! 많은 감사.
친구는 위치의 너의 현재 팬이 되었다!
아주 좋은 나는 위치 그것을 감사 좋아한다!
이 위치는 아니라 유익한뿐 재미있는다!
걸출한 뉴스!! 종류 블로그!
일! 우수한 감사!
걸출한 디자인! 좋은 디자인.
유용한 정보. 좋은 디자인.
유용한 정보. 좋은 디자인.