Hướng dẫn viết một chương trình tính toán song song đơn giản

Bài toán: Tính tổng từ 1 tới numnodes*10^8.

Trong đó: numnodes là số processros bạn khai báo trong quá trình xử lý.

Code: tinhsum_new.cpp

#include <iostream>
#include <time.h> // Thu vien chua ham tinh thoi gian chay cua mot chuong trinh
#include <stdio.h> // Thu vien chua ham in dinh dang printf
#include <mpi.h> // Thu vien dung cho TINH TOAN SONG SONG

using namespace std;

/*
! This program shows how to caculate all the integer numbers from 1 to numnodes*10^8 by using numnodes processors
*/

/* globals */
int numnodes,myid,mpi_err;

clock_t startwtime, endwtime; // Bien dung de tinh toan thoi gian chay cua chuong trinh

#define mpi_root 0
#define VALUE 2E8
#define ull unsigned long long
/* end globals  */

void init_it(int  *argc, char ***argv) {
	mpi_err = MPI_Init(argc,argv);
    mpi_err = MPI_Comm_size( MPI_COMM_WORLD, &numnodes );
    mpi_err = MPI_Comm_rank(MPI_COMM_WORLD, &myid);
}

int main(int argc,char *argv[]){
	ull *back_ray;
	ull i,start,finish,total, value = VALUE;

	init_it(&argc,&argv);

/* create the data on the root */
	if(myid == mpi_root){
            startwtime = clock();
            back_ray= new ull[numnodes];
		}

/* each processor does a local sum */
	total=0;
	start = 1;
	finish = start + value;
	for(i=start;i<finish;i++) total=total+i;
	printf("...the Processor %2d caculate from %2dE8 + 1 to %2dE8: total = %18llu\n", myid, myid, myid+1, total);
	//cout << "...the Processor " << myid << " caculate from " << myid << "E8 + 1 to " << myid+1 << "E8: total = " << total << endl;

/* send the local sums back to the root */
    mpi_err = MPI_Gather(&total,    1,  MPI_UNSIGNED_LONG_LONG, // Gui lai processor chinh gia tri kieu : unsigned long long
						back_ray,   1,  MPI_UNSIGNED_LONG_LONG, // Processor chinh nhan lai gia tri kieu: unsigned long long
	                 	mpi_root, // Day la processor chinh
	                 	MPI_COMM_WORLD);

/* the root prints the global sum */
	if(myid == mpi_root){
	  total=0;
	  for(i=0;i<numnodes;i++) total=total+back_ray[i];
	  cout << "RESULT FROM ALL PROCESSORS = " << total << endl;

	  endwtime = clock();
	  //printf("TIME FOR RUNNING THE PROGRAM: %0.3lf s", (double) (endwtime-startwtime)/CLOCKS_PER_SEC);
	  cout << "TIME FOR RUNNING THE PROGRAM: " << (double) (endwtime-startwtime)/CLOCKS_PER_SEC << " s" << endl;
	}

    mpi_err = MPI_Finalize();
}

Chạy trên hệ thống của Trung tâm Khoa học Tính toán

1. Dịch file

mpic++   tinhsum_new.cpp   -o   tinhsum_new

2. Tạo file tinhsum_new.script

#!/bin/sh
/usr/local/bin/mpiexec /home/son/Laptrinh/mpi/tinhtong_new/tinhsum_new

3. Tạo thêm file toson.sh với khai báo sử dụng 5 nodes, mỗi node sử dụng 2 processors (tổng cộng là 10 processors)

qsub -q l1 -l nodes=5:ppn=2 tinhsum_new.script

4. Cho phép file toson.sh có thể chạy

chmod u+x toson.sh

5. Chạy

./toson.sh
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: