-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add C convolution routine for speedup
-Also changes to structure of ring and unit hydrographs. Changes are internal and are backward compatable. -Fixed bug in agg_tsteps logic
- Loading branch information
Joe Hamman
committed
Feb 10, 2014
1 parent
eb662f1
commit 984c422
Showing
7 changed files
with
281 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
void c_convolve(const int nsources, /*scalar - number of sources*/ | ||
const int noutlets, /*scalar - length of subset*/ | ||
const int subset_length, /*scalar - length of subset*/ | ||
const int x_size, | ||
const int* source2outlet_ind, /*1d array - source to outlet mapping*/ | ||
const int* source_y_ind, /*1d array - source y location*/ | ||
const int* source_x_ind, /*1d array - source x location*/ | ||
const int* source_time_offset, /*1d array - source time offset*/ | ||
const double* unit_hydrograph, /*2d array[times][sources] - unit hydrographs*/ | ||
const double* aggrunin, /*2d array[ysize][xsize] - vic runoff flux*/ | ||
double* ring) /*2d array[times][outlets] - convolution ring*/ | ||
{ | ||
const double* runin; /*pointer to sources runoff flux*/ | ||
int s, i, j; /*counters*/ | ||
int y, x, offset, outlet; /*2d indicies*/ | ||
int xyind, rind, uhind; /*1d indicies*/ | ||
|
||
/*Loop through all sources*/ | ||
for (s = 0; s < nsources; s++) { | ||
|
||
outlet = source2outlet_ind[s]; | ||
y = source_y_ind[s]; | ||
x = source_x_ind[s]; | ||
offset = source_time_offset[s]; | ||
|
||
//1d index location | ||
//2d-->1d indexing goes like this: ind = y*x_size + x | ||
xyind = y*x_size + x; | ||
|
||
runin = &aggrunin[xyind]; | ||
|
||
// if (*runin > 10000.0) { | ||
// printf("runin is big: %f\n", *runin); | ||
// printf("xyind: %i\n", xyind); | ||
// printf("y: %i\n", y); | ||
// printf("x: %i\n", x); | ||
|
||
/* Do the convolution */ | ||
for (i = 0; i < subset_length; i++) { | ||
j = i + offset; | ||
|
||
//1d index locations | ||
rind = j * noutlets + outlet; | ||
uhind = i * nsources + s; | ||
|
||
ring[rind] += unit_hydrograph[uhind] * *runin; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
""" | ||
convolution_wrapper.py | ||
ctypes wrapper for convolve.c | ||
gcc -shared -o c_convolve.so c_convolve.c | ||
""" | ||
|
||
import numpy as np | ||
import ctypes | ||
|
||
import os | ||
# os.system('gcc -shared -o c_convolve.so c_convolve.c') | ||
|
||
try: | ||
path_to_file = os.path.split(__file__)[0] | ||
|
||
_convolution = np.ctypeslib.load_library('c_convolve.so', path_to_file) | ||
args = [ctypes.c_int, | ||
ctypes.c_int, | ||
ctypes.c_int, | ||
ctypes.c_int, | ||
np.ctypeslib.ndpointer(np.int32), | ||
np.ctypeslib.ndpointer(np.int32), | ||
np.ctypeslib.ndpointer(np.int32), | ||
np.ctypeslib.ndpointer(np.int32), | ||
np.ctypeslib.ndpointer(np.float64), | ||
np.ctypeslib.ndpointer(np.float64), | ||
np.ctypeslib.ndpointer(np.float64)] | ||
_convolution.c_convolve.argtypes = args | ||
_convolution.c_convolve.restype = None | ||
|
||
def rvic_convolve(*args): | ||
"""args: | ||
nsources, | ||
noutlets, | ||
subset_length, | ||
xsize, | ||
source2outlet_ind, | ||
source_y_ind, | ||
source_x_ind, | ||
source_time_offset, | ||
unit_hydrograph, | ||
aggrunin, | ||
ring | ||
""" | ||
_convolution.c_convolve(*args) | ||
|
||
return | ||
except: | ||
print('Using brodcasting convolution method because there was a problem ' | ||
'loading c_convolve.c') | ||
|
||
def rvic_convolve(nsources, noutlets, subset_length, xsize, | ||
source2outlet_ind, source_y_ind, source_x_ind, | ||
source_time_offset, unit_hydrograph, aggrunin, ring): | ||
# numpy brodcasting method | ||
for s, outlet in enumerate(source2outlet_ind): | ||
y = source_y_ind[s] | ||
x = source_x_ind[s] | ||
i = source_time_offset[s] | ||
j = i + subset_length | ||
ring[i:j, outlet] += np.squeeze(unit_hydrograph[:, s] * aggrunin[y, x]) | ||
|
||
# # pure python convolution | ||
# for s, outlet in enumerate(source2outlet_ind): | ||
# y = source_y_ind[s] | ||
# x = source_x_ind[s] | ||
# for i in xrange(subset_length): | ||
# j = i + source_time_offset[s] | ||
# ring[j, outlet] += (unit_hydrograph[i, s] * aggrunin[y, x]) | ||
return | ||
|
||
|
||
def test(): | ||
nsources = 20 | ||
subset_length = 10 | ||
full_time_length = 15 | ||
noutlets = 4 | ||
source2outlet_ind = np.linspace(0, noutlets, nsources, | ||
endpoint=False).astype(np.int32) | ||
|
||
ysize = 12 | ||
xsize = 15 | ||
|
||
source_y_ind = np.random.randint(0, ysize-1, nsources).astype(np.int32) | ||
source_x_ind = np.random.randint(0, xsize-1, nsources).astype(np.int32) | ||
|
||
source_time_offset = np.random.randint(0, 4, nsources).astype(np.int32) | ||
|
||
aggrunin = np.random.uniform(size=(ysize, xsize)) | ||
unit_hydrograph = np.zeros((subset_length, nsources), dtype=np.float64) | ||
unit_hydrograph[0:4, :] = 0.25 | ||
ring = np.zeros((full_time_length, noutlets), dtype=np.float64) | ||
|
||
for i in xrange(10): | ||
aggrunin = np.random.uniform(size=(ysize, xsize)) | ||
rvic_convolve(nsources, noutlets, subset_length, xsize, | ||
source2outlet_ind, source_y_ind, source_x_ind, | ||
source_time_offset, unit_hydrograph, aggrunin, ring) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.