Mã nguồn ứng dụng

Một phần của tài liệu LUẬN VĂN: NGHIÊN CỨU HỆ THỐNG CỤM MÁY TÍNH XỬ LÝ SONG SONG ROCKS VÀ ỨNG DỤNG pptx (Trang 91 - 98)

4. 15 Phƣơng hƣớng xây dựng một ứng dụng MPI

5.2.Mã nguồn ứng dụng

Dƣới đây chỉ trình bày mã nguồn cùng giải thích sơ lƣợc. Chi tiết hơn có thể xem trong mã nguồn ứng dụng đi kèm tài liệu này.

#include <dirent.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "mpi.h" #include "cv.h" #include "highgui.h" #define END_TAG 99 #define MASTER 0 #define PREFIX "out-"

/**

80

* @param error Ma loi

* @param rank Rank cua tien trinh xay ra loi * @param functionName Ham xay ra loi

*/

void ErrorMessage(int error, int rank, char* functionName) {

fprintf(stderr, "Process %d: Error %d in %s\n", rank, error, functionName);

MPI_Abort(MPI_COMM_WORLD, -1); }

/**

* Convert video

* @param in Ten file dau vao * @param out Ten file dau ra */

void convert(char* in, char* out) {

CvCapture* capture = 0;

// Mo file de bat dau doc

capture = cvCreateFileCapture(in); if (!capture) {

return; }

// Input frame.

IplImage* bgr_frame = cvQueryFrame(capture); // Lay cac properties quan trong

double fps = cvGetCaptureProperty( capture,

CV_CAP_PROP_FPS );

// Kich thuoc cua video output. O day ta giu nguyen kich thuoc // cua video input.

CvSize size = cvSize(

(int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT) );

81

// Tao mot object CvVideoWriter de bat dau ghi file.

// Tham so out la ten file de ghi, tham so thu 2 cho biet CODEC // cua video muon ghi, tham so thu 3 la thoi gian delay giua 2 // frame (delay frame rate), tham so cuoi cung la kich thuoc cua // moi frame.

CvVideoWriter* writer = cvCreateVideoWriter( out, CV_FOURCC('M', 'J', 'P', 'G'), fps, size ); // Output frame.

IplImage* logpolar_frame = cvCreateImage( size,

IPL_DEPTH_8U, 3 (adsbygoogle = window.adsbygoogle || []).push({});

);

// Doc lan luot cac frame

while((bgr_frame = cvQueryFrame(capture)) != NULL) {

// Nen frame vua doc duoc theo codec moi cvLogPolar( bgr_frame, logpolar_frame, cvPoint2D32f( bgr_frame -> width / 2, bgr_frame -> height / 2 ), 40, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS );

// Ghi frame sau khi nen

cvWriteFrame(writer, logpolar_frame); }

// Release cac tai nguyen cvReleaseVideoWriter(&writer);

82 cvReleaseImage(&logpolar_frame); cvReleaseCapture(&capture); } /** * Ham chinh */

int main(int argc, char* argv[]) {

int numProcs;// So luong tien trinh

int myRank; // Rank cua tien trinh hien tai

int error = 0;// Ma loi

int i;

MPI_Status status;// Trang thai thuc hien ham MPI

char* buffer; // Bo nho tam char* fileToSend; // Ten file gui di char* fileReceived; // Ten file nhan duoc char* fileToWrite; // Ten file output

DIR *d;// Thu muc chua cac video // can convert

struct dirent *dir;// Mot file / thu muc con

// trong thu muc chua cac video

int inFileNameLength;// Kich thuoc thuc su cua ten file input

// Master quan niem mot tien trinh da "finished" theo nghia tien // trinh do hien o trang thai "roi" va khong con video nao can // convert de gui cho tien trinh do nua

int numFinished = 0;// So luong tien trinh roi.

MPI_Init(&argc, &argv);

MPI_Comm_size(MPI_COMM_WORLD, &numProcs); MPI_Comm_rank(MPI_COMM_WORLD, &myRank);

83

buffer = (char*) malloc (256 * sizeof(char)); if (buffer == NULL) {

ErrorMessage(-1, myRank, "malloc"); }

if (myRank == MASTER) {

d = opendir("."); if (d) {

// Nhan dien cac file trong thu muc roi gui cho // cac slave convert.

for (i = 1; i < numProcs; ++i) { if ((dir = readdir(d)) != NULL) { (adsbygoogle = window.adsbygoogle || []).push({});

fileToSend = dir -> d_name;

error= MPI_Send((void*)fileToSend, strlen(fileToSend) + 1, MPI_CHAR, i, MASTER, MPI_COMM_WORLD);

if (error != MPI_SUCCESS) {

ErrorMessage(error, myRank, "MPI_Send"); }

} else { break; }

}

// Nhan ket qua tra ve & tiep tuc gui video convert // neu con

while (numFinished < numProcs - 1) {

error = MPI_Recv(buffer, 128, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

if (error != MPI_SUCCESS) {

ErrorMessage(error, myRank, "MPI_Recv"); }

if ((dir = readdir(d)) != NULL) { // Neu con video de convert

84

fileToSend = dir -> d_name;

error = MPI_Send((void*)fileToSend, strlen(fileToSend) + 1, MPI_CHAR, status.MPI_SOURCE, MASTER, MPI_COMM_WORLD);

if (error != MPI_SUCCESS) {

ErrorMessage(error, myRank, "MPI_Send"); }

} else { // Neu het

++numFinished;

fileToSend = "";

error = MPI_Send(fileToSend, 0, MPI_CHAR, status.MPI_SOURCE, END_TAG, MPI_COMM_WORLD);

if (error != MPI_SUCCESS) {

ErrorMessage(error, myRank, "MPI_Send"); }

} } } } else {

// Slave lien tuc nhan yeu cau tu server while (1) {

error = MPI_Recv(buffer, 128, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

if (error != MPI_SUCCESS) {

ErrorMessage(error, myRank, "MPI_Recv"); }

if (status.MPI_TAG != END_TAG) {

// Lay kich thuoc thuc su cua ten file (adsbygoogle = window.adsbygoogle || []).push({});

error = MPI_Get_count(&status, MPI_CHAR, &inFileNameLength);

85

if (inFileNameLength == MPI_UNDEFINED) {

ErrorMessage(error, myRank, "MPI_Get_count"); }

// Tao moi ten file input va ten file output tu // du lieu nhan duoc

fileReceived = (char*) malloc((inFileNameLength + 1) * sizeof (char));

fileToWrite = (char*) malloc((inFileNameLength + strlen(PREFIX) + 1) * sizeof (char));

strncpy(fileReceived, buffer, inFileNameLength); strcpy(fileToWrite, PREFIX);

strcat(fileToWrite, fileReceived);

// Chi convert cac file co phan mo rong la avi if (strstr(buffer, "avi") != NULL) {

convert(fileReceived, fileToWrite); }

// Xong

fileToSend = "";

error = MPI_Send(fileToSend, 0, MPI_CHAR, MASTER, END_TAG, MPI_COMM_WORLD);

if (error != MPI_SUCCESS) {

ErrorMessage(error, myRank, "MPI_Send"); } } else { break; } } } MPI_Finalize(); return 0; }

86

Một phần của tài liệu LUẬN VĂN: NGHIÊN CỨU HỆ THỐNG CỤM MÁY TÍNH XỬ LÝ SONG SONG ROCKS VÀ ỨNG DỤNG pptx (Trang 91 - 98)