Jump to ContentJump to Main Navigation
Parallel Scientific ComputationA Structured Approach using BSP and MPI$

Rob H. Bisseling

Print publication date: 2004

Print ISBN-13: 9780198529392

Published to Oxford Scholarship Online: September 2007

DOI: 10.1093/acprof:oso/9780198529392.001.0001

Show Summary Details
Page of

PRINTED FROM OXFORD SCHOLARSHIP ONLINE (www.oxfordscholarship.com). (c) Copyright Oxford University Press, 2017. All Rights Reserved. Under the terms of the licence agreement, an individual user may print out a PDF of a single chapter of a monograph in OSO for personal use (for details see http://www.oxfordscholarship.com/page/privacy-policy). Subscriber: null; date: 25 February 2017

(p.251) APPENDIX A AUXILIARY BSPEDUPACK FUNCTIONS

(p.251) APPENDIX A AUXILIARY BSPEDUPACK FUNCTIONS

Source:
Parallel Scientific Computation
Publisher:
Oxford University Press

A.1 Header file bspedupack.h

This header file is included in every program file of BSPedupack. It contains necessary file inclusions, useful definitions and macros, and prototypes for the memory allocation and deallocation functions.

BSPedupack Version 1.0

Copyright (C) 2004 Rob H. Bisseling

BSPedupack is released under the GNU GENERAL PUBLIC LICENSE

Version 2, June 1991 (given in the file LICENSE)

/*

BSPedupack Version 1.0

Copyright (C) 2004 Rob H. Bisseling

BSPedupack is released under the GNU GENERAL PUBLIC LICENSE

Version 2, June 1991 (given in the file LICENSE)

*/

#include <stdio·h>

#include <stdlib·h>

#include <math·h>

#include “bsp.h”

#define SZDBL (sizeof(double))

#define SZINT (sizeof(int))

#define TRUE (1)

#define FALSE (0)

#define MAX(a, b) ((a)>(b) ? (a): (b))

#define MIN(a, b) ((a)<(b) ? (a): (b))

double *vecallocd(int n);

int *vecalloci(int n);

double **matallocd(int m, int n);

void vecfreed(double *pd);

void vecfreei(int *pi);

void matfreed(double **ppd);

A.2 Utility file bspedupack.c

This file contains the functions used by BSPedupack to allocate and deallocate memory dynamically for storing vectors and matrices. The functions are suited for use in a parallel program, because they can abort a parallel computation (p.252) on all processors if they detect a potential memory overflow on one of the processors. If zero memory space is requested, which could happen if the local part of a distributed vector or matrix is empty, a NULL pointer is returned and nothing else is done.

Allocating an m × n matrix reserves a contiguous chunk of mn memory cells and prepares it for row-wise matrix access. As a result, matrix element a ij can conveniently be addressed as a[i][j] and row i as a[i].

#include “bspedupack.h”

/* These functions can be used to allocate and deallocate vectors and matrices. If not enough memory available, one processor halts them all.

*/

double *vecallocd(int n){

/* This function allocates a vector of doubles of length n */

double *pd;

if (n==0){

pd= NULL;

} else {

pd= (double *)malloc(n*SZDBL);

if (pd==NULL)

bsp_abort(“vecallocd: not enough memory”);

}

return pd;

} /* end vecallocd */

int *vecalloci(int n){

/* This function allocates a vector of integers of length n */

int *pi;

if (n==0){

pi= NULL;

} else {

pi= (int *)malloc(n*SZINT);

if (pi==NULL)

bsp_abort(“vecalloci: not enough memory”);

}

return pi;

} /* end vecalloci */

double **matallocd(int m, int n){

/* This function allocates anmxn matrix of doubles */

int i;

double *pd, **ppd;

if (m==0){

ppd= NULL;

(p.253)

} else {

ppd= (double **)malloc(m*sizeof(double *));

if (ppd==NULL)

bsp_abort(″matallocd: not enough memory″);

if (n==0){

for (i=0; i<m; i++)

ppd[i]= NULL;

} else {

pd= (double *)malloc(m*n*SZDBL);

if (pd==NULL)

bsp_abort(″matallocd: not enough memory″);

ppd[0]=pd;

for (i=1; i<m; i++)

ppd[i]= ppd[i−1]+n;

}

}

return ppd;

} /* end matallocd */

void vecfreed(double *pd){

/* This function frees a vector of doubles */

if (pd!=NULL)

free(pd);

} /* end vecfreed */

void vecfreei(int *pi){

/* This function frees a vector of integers */

if (pi!=NULL)

free(pi);

} /* end vecfreei */

void matfreed(double **ppd){

/* This function frees a matrix of doubles */

if (ppd!=NULL){

if (ppd[0]!=NULL)

free(ppd[0]);

free(ppd);

}

} /* end matfreed */