/*****************************************************************************/
/* */
/* Copyright (c) 2008, 2009, 2010 */
/* Computer Architecture Group (CAG) */
/* University of A Coruña, Spain */
/* (http://gac.des.udc.es) */
/* Galicia Supercomputing Center (CESGA) */
/* (http://www.cesga.es) */
/* Hewlett-Packard Spain (HP) */
/* (http://www.hp.es) */
/* */
/* This file is part of UPC Operations Microbenchmarking Suite (UOMS). */
/* */
/* UOMS is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU Lesser General Public License as published */
/* by the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* UOMS is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with UOMS. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*****************************************************************************/
/*****************************************************************************/
/* */
/* FUNDING: This development has been funded by Hewlett-Packard Spain */
/* */
/* Project Name: */
/* UPCHACO (2008-2011) */
/* Subproject: */
/* Improving UPC Usability and Performance in Constellation Systems: */
/* Implementation/Extensions of UPC Libraries. */
/* (UPCPUProject -> UPC Performance and Usability Project) */
/* */
/*****************************************************************************/
/*****************************************************************************
For further documentation, see
[1] Files under doc/
******************************************************************************/
#include <upc.h>
#include <stdlib.h>
#include <stdio.h>
#include "defines.h"
#include "headers.h"
/*
Distributed array used in various benchmarks
*/
extern shared char *distArr;
/*
Broadcast array
*/
extern shared [] char *broadcastArr;
/*
Scatter array
*/
extern shared [] char *scatterArr;
/*
Gather array
*/
extern shared [] char *gatherArr;
/*
Gatherall array
*/
extern shared char *gatherallArr;
/*
Exchange array
*/
extern shared char *exchangeArr;
/*
Permute array
*/
extern shared char *permuteArr;
/*
Reduce array (will store only 1 element)
*/
extern shared [] char *reduceArr;
/*
Prefix reduce array
*/
extern shared char *prefixReduceArr;
/*
Pointer for memory allocation and freeing test
*/
extern shared char *mem_alloc_tests_pointer;
/*
Arrays for p2p benchmarking
*/
extern shared char *p2pDistArr;
extern shared char *p2pDistArr2;
extern char *p2pLocalArr;
extern char *p2pLocalArr2;
int allocate_arrays(int operation_code, long int cursize, long int nbuckets){
size_t nbytes = cursize*nbuckets;
int mem_is_ok;
/*
Setup p2p arrays
*/
if (p2poperation(operation_code)) {
p2pLocalArr = (char *) malloc(nbytes);
MEM_OK(p2pLocalArr);
if(mem_is_ok == -1) return -1;
if(operation_code == SMEMCPY || operation_code == MEMMOVE){
p2pLocalArr2 = (char *) malloc(nbytes);
MEM_OK(p2pLocalArr2);
if(mem_is_ok == -1) return -1;
}
else{
p2pDistArr = upc_all_alloc(THREADS,nbytes);
UPCMEM_OK(p2pDistArr);
if(mem_is_ok == -1) return -1;
#if defined (ASYNC_MEM_TEST) || defined (ASYNCI_MEM_TEST)
if(operation_code == MEMCPY || operation_code == LMEMCPY ||
operation_code == AMEMCPY || operation_code == ALMEMCPY ||
operation_code == AIMEMCPY || operation_code == AILMEMCPY){
#else
if(operation_code == MEMCPY || operation_code == LMEMCPY){
#endif
p2pDistArr2 = upc_all_alloc(THREADS,nbytes);
UPCMEM_OK(p2pDistArr);
if(mem_is_ok == -1) return -1;
}
}
}
/*
Setup collective arrays
*/
else{
size_t dist_blk_size = 0;
size_t num_dist_blk = 0;
if(operation_code == BROADCAST){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
broadcastArr = upc_all_alloc(1,nbytes);
UPCMEM_OK(broadcastArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == SCATTER){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
scatterArr = upc_all_alloc(1,nbytes*THREADS);
UPCMEM_OK(scatterArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == GATHER){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
gatherArr = upc_all_alloc(1,nbytes*THREADS);
UPCMEM_OK(gatherArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == GATHERALL){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
gatherallArr = upc_all_alloc(num_dist_blk,cursize*THREADS);
UPCMEM_OK(gatherallArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == EXCHANGE){
dist_blk_size = cursize*THREADS;
num_dist_blk = THREADS*nbuckets;
exchangeArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(exchangeArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PERMUTE){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
permuteArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(permuteArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_C){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(char));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_C){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_UC){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(unsigned char));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_UC){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_S){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(short));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_S){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_US){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(unsigned short));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_US){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_I){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(int));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_I){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_UI){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(unsigned int));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_UI){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_L){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(long));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_L){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_UL){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(unsigned long));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_UL){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_F){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(float));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_F){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_D){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(double));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_D){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == REDUCE_LD){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
reduceArr = upc_all_alloc(1,sizeof(long double));
UPCMEM_OK(reduceArr);
if(mem_is_ok == -1) return -1;
}
else if(operation_code == PREFIX_REDUCE_LD){
dist_blk_size = cursize;
num_dist_blk = THREADS*nbuckets;
prefixReduceArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(prefixReduceArr);
if(mem_is_ok == -1) return -1;
}
if(operation_code != BARRIER &&
operation_code != ALLALLOC &&
operation_code != FREE){
distArr = upc_all_alloc(num_dist_blk,dist_blk_size);
UPCMEM_OK(distArr);
if(mem_is_ok == -1) return -1;
}
}
return 0;
}
void free_arrays(int operation_code){
/*
Free p2p arrays
*/
if (p2poperation(operation_code)) {
free(p2pLocalArr);
if(operation_code == SMEMCPY || operation_code == MEMMOVE){
free(p2pLocalArr2);
}
else{
if (MYTHREAD == 0){
upc_free(p2pDistArr);
#if defined (ASYNC_MEM_TEST) || defined (ASYNCI_MEM_TEST)
if(operation_code == MEMCPY || operation_code == LMEMCPY ||
operation_code == AMEMCPY || operation_code == ALMEMCPY ||
operation_code == AIMEMCPY || operation_code == AILMEMCPY){
#else
if(operation_code == MEMCPY || operation_code == LMEMCPY){
#endif
upc_free(p2pDistArr2);
}
}
}
}
/*
Free collective arrays
*/
else{
if(MYTHREAD == 0){
if(operation_code == BROADCAST){
upc_free(broadcastArr);
}
else if(operation_code == SCATTER){
upc_free(scatterArr);
}
else if(operation_code == GATHER){
upc_free(gatherArr);
}
else if(operation_code == GATHERALL){
upc_free(gatherallArr);
}
else if(operation_code == EXCHANGE){
upc_free(exchangeArr);
}
else if(operation_code == PERMUTE){
upc_free(permuteArr);
}
else if(operation_code == REDUCE_C
|| operation_code == REDUCE_UC
|| operation_code == REDUCE_S
|| operation_code == REDUCE_US
|| operation_code == REDUCE_I
|| operation_code == REDUCE_UI
|| operation_code == REDUCE_L
|| operation_code == REDUCE_UL
|| operation_code == REDUCE_F
|| operation_code == REDUCE_D
|| operation_code == REDUCE_LD){
upc_free(reduceArr);
}
else if(operation_code == PREFIX_REDUCE_C
|| operation_code == PREFIX_REDUCE_UC
|| operation_code == PREFIX_REDUCE_S
|| operation_code == PREFIX_REDUCE_US
|| operation_code == PREFIX_REDUCE_I
|| operation_code == PREFIX_REDUCE_UI
|| operation_code == PREFIX_REDUCE_L
|| operation_code == PREFIX_REDUCE_UL
|| operation_code == PREFIX_REDUCE_F
|| operation_code == PREFIX_REDUCE_D
|| operation_code == PREFIX_REDUCE_LD){
upc_free(prefixReduceArr);
}
if(operation_code != BARRIER &&
operation_code != ALLALLOC &&
operation_code != FREE){
upc_free(distArr);
}
}
}
}