#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <math.h>
extern HLRC_myself;
extern HLRC_nprocs;
extern HLRC_nlocks;
extern HLRC_init_flag;
#define NLOCKS (18*1024)
#define PROCESSOR_MHZ 650000
#define GETTIME(x) asm volatile (".byte 0x0f, 0x31" : "=A" (x));
#define N 1000
FILE *logfile;
double* px;
double* py;
double* vx;
double* vy;
double* fx;
double* fy;
double* m;
double G = 6.67;
int gridSize, stripSize, numWorkers, numIters;
void jacobi() {
int firstRow = (HLRC_myself) * stripSize +1;
int lastRow = firstRow + stripSize - 2;
double deltav_x, deltav_y;
double deltap_x, deltap_y;
double distance, magnitude;
double direction_x, direction_y;
double DT = 1;
int iter, i, j, x;
printf("\nIniciando iteracoes...");
printf("firstRow[%d] - lastRow[%d]\n", firstRow, lastRow);
for (iter=1; iter < numIters; iter++)
{
for(i = firstRow; i < lastRow; i++)
{
fx[i] = fy[i] = 0;
for(j=i+1; j<lastRow; j++)
{
direction_x = px[j]-px[i];
direction_y = py[j]-py[i];
distance = sqrt( (px[i] - px[j])*(px[i] - px[j]) + (py[i] - py[j])*(py[i] - py[j]) );
magnitude = (G*m[i]*m[j])/(distance*distance);
fx[i] = fx[i] + magnitude*direction_x/distance;
fx[j] = fx[j] + magnitude*direction_x/distance;
fy[i] = fy[i] + magnitude*direction_y/distance;
fy[j] = fy[j] + magnitude*direction_y/distance;
}
}
HLRC_Barrier();
for(x = firstRow; x < lastRow; x++)
{
deltav_x = fx[x]/m[x]*DT;
deltav_y = fy[x]/m[x]*DT;
deltap_x = fx[x]/m[x]*DT;
deltap_y = fy[x]/m[x]*DT;
vx[x] = vx[x] + deltav_x;
vy[x] = vy[x] + deltav_y;
px[x] = px[x] + deltap_x;
py[x] = py[x] + deltap_y;
fx[x] = fy[x] = 0;
}
HLRC_Barrier();
}
}
void iniciaJacobi() {
int i;
unsigned int start, end;
{
unsigned long long t;
GETTIME(t);
start = (unsigned int)(t/PROCESSOR_MHZ);
}
printf("Iniciando paralelismo\n");
HLRC_InitParallel();
printf("Iniciado\n");
jacobi();
{
HLRC_ClockStop();
HLRC_Barrier();
}
{
unsigned long long t;
GETTIME(t);
end = (unsigned int)(t/PROCESSOR_MHZ);
}
printf("Elapsed time (cycles): %u \n", end-start);
}
int main(int argc, char **argv) {
unsigned int start, end;
unsigned long long t;
int i, j;
if ((argc <= 1) || (argc != 3)) {
printf("\n\n");
printf("Syntax:\n");
printf("jacobi-hlrc [gridSize][numIter]\n");
printf("gridSize: tamanho da matriz\n");
printf("numIter: numero de iteracoes\n\n");
return 1;
}
gridSize = atoi(argv[1]);
numIters = atoi(argv[2]);
HLRC_Init(argc, argv);
numWorkers = HLRC_nprocs;
stripSize = gridSize/numWorkers;
/*if (gridSize % numWorkers != 0) {
printf("\n\nO tamanho do grid deve ser multiplo do numero de processos!\n\n");
HLRC_End();
return 0;
}*/
printf("----------------\n");
printf("Tamanho Grid.: %d\n", gridSize);
printf("Trabalhadores: 1\n");
printf("Iteracoes....: %d\n", numIters);
printf("----------------\n");
printf("Alocando memória mas matrizes\n");
px = (double *) HLRC_Malloc((gridSize+1) * sizeof(double));
py = (double *) HLRC_Malloc((gridSize+1) * sizeof(double));
vx = (double *) HLRC_Malloc((gridSize+1) * sizeof(double));
vy = (double *) HLRC_Malloc((gridSize+1) * sizeof(double));
fx = (double *) HLRC_Malloc((gridSize+1) * sizeof(double));
fy = (double *) HLRC_Malloc((gridSize+1) * sizeof(double));
m = (double *) HLRC_Malloc((gridSize+1) * sizeof(double));
printf("Alocado\n");
printf("Preenchendo matrizes\n");
for (i=0; i < gridSize; i++) {
px[i] = 1;
py[i] = 1;
fx[i] = 1;
fy[i] = 1;
vx[i] = 1;
vy[i] = 1;
}
printf("Preenchido\n");
printf("Iniciando Jacobi\n");
iniciaJacobi();
printf("Barreira\n");
HLRC_Barrier();
printf("Encerrando\n");
HLRC_End();
}
jacobi-dsm.zip_dsm
版权申诉
26 浏览量
2022-09-14
17:44:32
上传
评论
收藏 1KB ZIP 举报
我虽横行却不霸道
- 粉丝: 75
- 资源: 1万+
最新资源
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
- 基于Dart的Flutter小提琴调音器APP设计源码 - violinhelper
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
- 基于Vue和微信小程序的监理日志系统设计源码 - supervisionLog
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈