Skip to content

Instantly share code, notes, and snippets.

@LightBells
Forked from riku1314/jikken.h
Last active June 21, 2020 14:11
Show Gist options
  • Save LightBells/d5c243c6e5387429beb05e276e4926ef to your computer and use it in GitHub Desktop.
Save LightBells/d5c243c6e5387429beb05e276e4926ef to your computer and use it in GitHub Desktop.
B-1
extern void make_matrix(unsigned int size, int flag,double ***a,double ***b,unsigned int number)
{
int i,j;
*a=(double**)malloc(sizeof(double*)*size);
*b=(double**)malloc(sizeof(double*)*size);
for(i=0;i<size;i++){
(*a)[i] = (double*)malloc(sizeof(double)*size);
(*b)[i] = (double*)malloc(sizeof(double)*size);
for(j=0;j<size;j++){
(*a)[i][j]=flag;
(*b)[i][j]=flag;
}
}
}
extern void check_matrix(unsigned int size, double **c, unsigned int number)
{
int i,j;
for(i=0;i<size;i++){
for(j=0;j<size;j++){
printf("%2d",c[i][j]);
}
putchar('\n');
}
}
#include <stdio.h>
#include <stdlib.h>
#include<omp.h>
#include "jikken.h"
int main(int argc, char *argv[])
{
/* ここに変数を定義します */
int size;
int i,j,k;
double start=0,end=0;
unsigned int number=1931165;
int flag=1;
if ((argc != 2) || (size=atoi(argv[1]))<=0) {
fprintf(stderr,"Invalid Argument\n");
exit(1);
}
double **a;
double **b;
double **c;
make_matrix(size,flag,&a,&b,number);
c=(double**)malloc(sizeof(double*)*size);
for(int i=0;i<size;i++){
c[i] = (double*)malloc(sizeof(double)*size);
}
/* ここまで実行すると、size に行列サイズが代入されます。*/
/* これより下にプログラムを作成してください。*/
start=omp_get_wtime();
#pragma omp parallel for
for(i=0;i<size;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];
}
}
}
end=omp_get_wtime();
check_matrix(size,c,number);
for (int i=0;i<size;i++){
free(a[i]);
free(b[i]);
free(c[i]);
}
free(a);
free(b);
free(c);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment