All pastes #885887 Raw Edit

Someone

public text v1 · immutable
#885887 ·published 2008-01-31 05:59 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mpi.h>

#define MASTER 0
#define IS_MASTER my_id==0

int total_procs, my_id;

typedef struct {
	int cols;
	int rows;
	float *data;
} MATRIX;

MATRIX create_matrix( int rows, int cols ) {
	MATRIX m;

	m.rows = rows;
	m.cols = cols;

	m.data = ( float * ) malloc( sizeof( float ) * rows * cols );

	return m;
}

void show_matrix( MATRIX *m ) {
	int i, j;
	
	for( i = 0; i < m->rows; i++ ) {
		for( j = 0; j < m->cols; j++ )
			printf( "%f ", m->data[i * m->cols + j] );
		printf( "\n" );
	}
}

void read_matrix( char *path, MATRIX *mat ) {
	int i, j;

	char aux_string[50];
	char rows_string[10], cols_string[10];

	FILE *input_file = fopen( path, "r" );

	fgets( aux_string, 50, input_file );
	printf( "Leyendo la matriz: %s", aux_string );

	fgets( aux_string, 50, input_file );
	sscanf( aux_string, "%s %s", rows_string, cols_string );

	printf( "dimensiones: %s x %s\n\n", rows_string, cols_string );

	*mat = create_matrix( atoi( rows_string ), atoi( cols_string ) );

	for( i = 0; i < mat->rows; i++ )
		for( j = 0; j < mat->cols; j++ )
			fscanf( input_file, "%f", &mat->data[i * mat->cols + j] );

	
}

void broadcast_matrix( MATRIX *m ) {
	int rows, cols;

	rows = m->rows;
	cols = m->cols;

	MPI_Bcast( &rows, 1, MPI_INT, MASTER, MPI_COMM_WORLD );
	MPI_Bcast( &cols, 1, MPI_INT, MASTER, MPI_COMM_WORLD );
	
	if(IS_MASTER)
		MPI_Bcast( m->data, rows * cols, MPI_FLOAT, MASTER, MPI_COMM_WORLD );
	else {
		*m = create_matrix( rows, cols );
		MPI_Bcast( m->data, rows * cols, MPI_FLOAT, MASTER, MPI_COMM_WORLD );
	}
}

void SOR_matrix_data( MATRIX m ) {
	MATRIX aux_matrix;
	MPI_Status status;
	int mod, div;
	int i, size, current_pos;

	div = m.rows / total_procs;
	mod = m.rows % total_procs;

	aux_matrix = m;
	
	if( IS_MASTER ) {
		current_pos = div;
		for( i = 1; i < total_procs; i++ ) {
			aux_matrix = create_matrix( div, aux_matrix.cols );
			memcpy( aux_matrix.data, m.data, sizeof( float ) * div * m.cols );
			size = div;
			if( i <= mod )
				size++;
			if( size > 0 )
				MPI_Send( m.data + ( current_pos * m.cols ), size * m.cols, MPI_FLOAT, i, 111, MPI_COMM_WORLD );
			current_pos += size;
		}
	}
	else {
		if( my_id <= mod )
			size++;
		aux_matrix = create_matrix( size, m.cols );
		if( size > 0 )
			MPI_Recv( aux_matrix.data, aux_matrix.rows * aux_matrix.cols, MPI_INT, MASTER, 111, MPI_COMM_WORLD, &status );
	}
}

int main( int argc, char *argv[] ) {
	MATRIX mat1, mat2;
	MPI_Status status;

	/*
	if(argc != 3) {
		printf("Uso del programa: nombre_del_ejecutable 1ra_matriz 2da_matriz\n");

		return 1;
	}*/
	
	MPI_Init( &argc, &argv );
	MPI_Comm_size( MPI_COMM_WORLD, &total_procs );
	MPI_Comm_rank( MPI_COMM_WORLD, &my_id );

	if(IS_MASTER) {
		read_matrix( "mat1", &mat1 );
		read_matrix( "mat2", &mat2 );

		show_matrix( &mat1 );
		printf("\n");
		show_matrix( &mat2 );
	}

	broadcast_matrix( &mat2 );
	SOR_matrix_data( mat1 );
	
	MPI_Finalize();

	return 0;
}