2006-10-18 22:58:49 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
|
|
|
//
|
|
|
|
// This 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 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This software 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.
|
|
|
|
//
|
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
2007-10-09 11:35:47 +00:00
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-01-03 20:35:32 +00:00
|
|
|
|
2005-07-14 16:46:38 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
|
2005-01-03 20:35:32 +00:00
|
|
|
#include "boinc_win.h"
|
2005-11-21 18:34:44 +00:00
|
|
|
#else
|
|
|
|
#include "config.h"
|
2005-07-14 16:46:38 +00:00
|
|
|
#endif
|
|
|
|
|
2005-01-03 20:35:32 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cmath>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "reduce.h"
|
|
|
|
|
|
|
|
// Prepare to receive a source array.
|
|
|
|
// (sx, sy) are dimensions of source array
|
|
|
|
//
|
2007-10-22 23:12:42 +00:00
|
|
|
void REDUCED_ARRAY_GEN::init_data(int sx, int sy) {
|
2005-01-03 20:35:32 +00:00
|
|
|
sdimx = sx;
|
|
|
|
sdimy = sy;
|
2007-10-22 23:12:42 +00:00
|
|
|
rdimx = sx;
|
|
|
|
rdimy = sy;
|
|
|
|
while (rdimx*rdimy > MAX_DATA) {
|
|
|
|
if (rdimx>1) rdimx /= 2;
|
|
|
|
if (rdimy>1) rdimy /= 2;
|
2005-01-03 20:35:32 +00:00
|
|
|
}
|
|
|
|
nvalid_rows = 0;
|
|
|
|
scury = 0;
|
|
|
|
last_ry = 0;
|
|
|
|
last_ry_count = 0;
|
|
|
|
rdata_max = 0;
|
|
|
|
rdata_min = (float)1e20;
|
|
|
|
}
|
|
|
|
|
2007-10-22 23:12:42 +00:00
|
|
|
bool REDUCED_ARRAY_GEN::full() {
|
2005-01-03 20:35:32 +00:00
|
|
|
return nvalid_rows==rdimy;
|
|
|
|
}
|
|
|
|
|
2007-10-22 23:12:42 +00:00
|
|
|
#if 0
|
|
|
|
void REDUCED_ARRAY_GEN::reset() {
|
2005-01-03 20:35:32 +00:00
|
|
|
nvalid_rows = 0;
|
|
|
|
ndrawn_rows = 0;
|
|
|
|
scury = 0;
|
|
|
|
last_ry = 0;
|
|
|
|
last_ry_count = 0;
|
|
|
|
}
|
2007-10-22 23:12:42 +00:00
|
|
|
#endif
|
2005-01-03 20:35:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
// reduce a single row. This is called only if sdimx > rdimx;
|
|
|
|
//
|
2007-10-22 23:12:42 +00:00
|
|
|
void REDUCED_ARRAY_GEN::reduce_source_row(float* in, float* out) {
|
2005-01-03 20:35:32 +00:00
|
|
|
int i, ri;
|
|
|
|
|
|
|
|
memset(out, 0, rdimx*sizeof(float));
|
|
|
|
memset(itemp, 0, rdimx*sizeof(int));
|
|
|
|
for (i=0; i<sdimx; i++) {
|
|
|
|
ri = (i*rdimx)/sdimx;
|
|
|
|
switch (reduce_method) {
|
|
|
|
case REDUCE_METHOD_AVG:
|
|
|
|
out[ri] += in[i];
|
|
|
|
itemp[ri]++;
|
|
|
|
break;
|
|
|
|
case REDUCE_METHOD_SUM:
|
|
|
|
out[ri] += in[i];
|
|
|
|
break;
|
|
|
|
case REDUCE_METHOD_MIN:
|
|
|
|
out[ri] = std::min(out[ri], in[i]);
|
|
|
|
break;
|
|
|
|
case REDUCE_METHOD_MAX:
|
|
|
|
out[ri] = std::max(out[ri], in[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (reduce_method==REDUCE_METHOD_AVG) {
|
|
|
|
for (i=0; i<rdimx; i++) {
|
|
|
|
if (itemp[i] > 1) out[i] /= itemp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-22 23:12:42 +00:00
|
|
|
void REDUCED_ARRAY_GEN::update_max(int row) {
|
2005-01-03 20:35:32 +00:00
|
|
|
int i;
|
|
|
|
float* p = rrow(row);
|
|
|
|
|
|
|
|
for (i=0; i<rdimx; i++) {
|
|
|
|
if (p[i] > rdata_max) rdata_max = p[i];
|
|
|
|
if (p[i] < rdata_min) rdata_min = p[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a row of data from the source array
|
|
|
|
//
|
2007-10-22 23:12:42 +00:00
|
|
|
void REDUCED_ARRAY_GEN::add_source_row(float* in) {
|
2005-01-03 20:35:32 +00:00
|
|
|
float* p;
|
|
|
|
int i, ry;
|
|
|
|
|
|
|
|
if (scury >= sdimy) {
|
|
|
|
printf("too many calls to add_source_row()!\n");
|
|
|
|
*(int*)0 = 0;
|
|
|
|
}
|
|
|
|
if (rdimy == sdimy) {
|
|
|
|
ry = scury;
|
|
|
|
if (rdimx == sdimx) {
|
|
|
|
memcpy(rrow(ry), in, rdimx*sizeof(float));
|
|
|
|
} else {
|
|
|
|
reduce_source_row(in, rrow(ry));
|
|
|
|
}
|
|
|
|
update_max(ry);
|
|
|
|
nvalid_rows++;
|
|
|
|
} else {
|
|
|
|
ry = (scury*rdimy)/sdimy;
|
|
|
|
if (scury == 0) memset(rrow(0), 0, rdimx*sizeof(float));
|
|
|
|
|
|
|
|
// if we've moved into a new row, finish up the previous one
|
|
|
|
//
|
|
|
|
if (ry > last_ry) {
|
|
|
|
p = rrow(last_ry);
|
|
|
|
if (last_ry_count > 1) {
|
|
|
|
for (i=0; i<rdimx; i++) {
|
|
|
|
p[i] /= last_ry_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update_max(last_ry);
|
|
|
|
nvalid_rows++;
|
|
|
|
last_ry = ry;
|
|
|
|
last_ry_count = 0;
|
|
|
|
memset(rrow(ry), 0, rdimx*sizeof(float));
|
|
|
|
}
|
|
|
|
|
|
|
|
last_ry_count++;
|
|
|
|
p = rrow(ry);
|
|
|
|
if (rdimx == sdimx) {
|
|
|
|
for (i=0; i<sdimx; i++) {
|
|
|
|
p[i] += in[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reduce_source_row(in, ftemp);
|
|
|
|
for (i=0; i<rdimx; i++) {
|
|
|
|
p[i] += ftemp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this is last row, finish up
|
|
|
|
//
|
|
|
|
if (scury == sdimy-1) {
|
|
|
|
p = rrow(last_ry);
|
|
|
|
if (last_ry_count > 1) {
|
|
|
|
for (i=0; i<rdimx; i++) {
|
|
|
|
p[i] /= last_ry_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update_max(ry);
|
|
|
|
nvalid_rows++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scury++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *BOINC_RCSID_70f1fa52c7 = "$Id$";
|