improved indentation, added exeption when not all inputs have the same ndims,…

improved indentation, added exeption when not all inputs have the same ndims, tried to improve some memory / reference counting issues, removed initialisation of variable sized arrays, which causes problems under windows
上级 043bc3e8
...@@ -2942,6 +2942,10 @@ class GpuJoin(tensor.Join, GpuOp): ...@@ -2942,6 +2942,10 @@ class GpuJoin(tensor.Join, GpuOp):
out[0] = rval out[0] = rval
def c_code(self, node, name, inputs, out_, sub): def c_code(self, node, name, inputs, out_, sub):
nd = node.inputs[1].ndim
if not all(i.ndim == nd for i in node.inputs[2:]):
# all inputs ndarray need to have the same number of dimensions
raise NotImplementedError()
axis = inputs[0] axis = inputs[0]
n_cndas = len(inputs[1:]) n_cndas = len(inputs[1:])
input_1 = inputs[1] input_1 = inputs[1]
...@@ -2951,38 +2955,36 @@ class GpuJoin(tensor.Join, GpuOp): ...@@ -2951,38 +2955,36 @@ class GpuJoin(tensor.Join, GpuOp):
# getting the shapes of all the involved tensors (input[0]+out) # getting the shapes of all the involved tensors (input[0]+out)
str = """ str = """
int axis = PyInt_AsLong((PyObject*)%(axis)s); int axis = PyInt_AsLong((PyObject*)%(axis)s);
int nd = CudaNdarray_NDIM(%(input_1)s); int nd = %(nd)s;
int shape_%(input_1)s[nd]; int shape_%(input_1)s[%(nd)s];
int shape_out[nd]; int shape_out[%(nd)s];
int width_sum = 0;
for(int i = 0; i<nd; i+=1) for(int i = 0; i<nd; i+=1)
{ {
shape_%(input_1)s[i] = CudaNdarray_HOST_DIMS(%(input_1)s)[i]; shape_%(input_1)s[i] = CudaNdarray_HOST_DIMS(%(input_1)s)[i];
shape_out[i] = shape_%(input_1)s[i]; shape_out[i] = shape_%(input_1)s[i];
} }
""" % locals() """ % locals()
# getting the shapes of all the involved tensors (input[1:]) # getting the shapes of all the involved tensors (input[1:])
# + check: all input tensors have same shape as final out # + check: all input tensors have same shape as final out
# execept for "axis" dimension # execept for "axis" dimension
for i, cdna in enumerate(inputs[2:]): for i, cdna in enumerate(inputs[2:]):
str += """ str += """
nd = CudaNdarray_NDIM(%(cdna)s); int shape_%(cdna)s[%(nd)s];
int shape_%(cdna)s[nd]; for(int i = 0; i<nd; i+=1)
for(int i = 0; i<nd; i+=1)
{
shape_%(cdna)s[i] = CudaNdarray_HOST_DIMS(%(cdna)s)[i];
if((i!=axis) && (shape_%(cdna)s[i]!=shape_out[i]))
{ {
// compilation error when `fail`-string is added shape_%(cdna)s[i] = CudaNdarray_HOST_DIMS(%(cdna)s)[i];
if((i!=axis) && (shape_%(cdna)s[i]!=shape_out[i]))
{
// compilation error when `fail`-string is added
}
} }
}
""" % locals() """ % locals()
# computing the new shape for the out tensors # computing the new shape for the out tensors
str += """
int width_sum = 0;\n""" % locals()
for i, cdna in enumerate(inputs[1:]): for i, cdna in enumerate(inputs[1:]):
...@@ -3000,58 +3002,59 @@ class GpuJoin(tensor.Join, GpuOp): ...@@ -3000,58 +3002,59 @@ class GpuJoin(tensor.Join, GpuOp):
step = NULL; step = NULL;
int errorcode; int errorcode;
int sum; int sum;
sum =0; sum = 0;
start = NULL;
PyObject *slice_tuple; PyObject *slice_tuple;
PyObject *full_slice;
PyObject *section_slice; PyObject *section_slice;
PyObject *full_slice;
full_slice = PySlice_New(NULL, NULL, NULL);
""" % locals() """ % locals()
# start copying the data into the new out tensors # start copying the data into the new out tensors
for i, cdna in enumerate(inputs[1:]): for i, cdna in enumerate(inputs[1:]):
str += """ str += """
sum += shape_%(cdna)s[axis]; sum += shape_%(cdna)s[axis];
stop = PyInt_FromLong(sum); Py_XDECREF(stop);
slice_tuple = PyTuple_New(nd); stop = PyInt_FromLong(sum);
full_slice = PySlice_New(NULL, NULL, NULL); slice_tuple = PyTuple_New(nd);
section_slice = PySlice_New(start, stop, step); section_slice = PySlice_New(start, stop, step);
for(int i=0; i<nd; i++) for(int i=0; i<nd; i++)
{
if(i!=axis)
{ {
Py_INCREF(full_slice); if(i!=axis)
PyTuple_SetItem(slice_tuple, i, full_slice); {
Py_INCREF(full_slice);
PyTuple_SetItem(slice_tuple, i, full_slice);
}
else
{
Py_INCREF(section_slice);
PyTuple_SetItem(slice_tuple, i, section_slice);
}
} }
else if(i==axis) out_sub = CudaNdarray_Subscript((PyObject*)%(out)s, slice_tuple);
errorcode = CudaNdarray_CopyFromCudaNdarray((CudaNdarray*)out_sub, %(cdna)s);
if((full_slice == NULL) || (section_slice == NULL) || (out_sub == NULL) || (errorcode != 0))
{ {
Py_INCREF(section_slice); Py_XDECREF(start);
PyTuple_SetItem(slice_tuple, i, section_slice); Py_XDECREF(stop);
Py_XDECREF(step);
Py_XDECREF(slice_tuple);
Py_XDECREF(out_sub);
Py_XDECREF(%(out)s);
%(fail)s;
} }
}
out_sub = CudaNdarray_Subscript((PyObject*)%(out)s, slice_tuple);
errorcode = CudaNdarray_CopyFromCudaNdarray((CudaNdarray*)out_sub, %(cdna)s);
if((full_slice == NULL) || (section_slice == NULL) || (out_sub == NULL) || (errorcode != 0))
{
Py_XDECREF(full_slice);
Py_XDECREF(section_slice);
Py_XDECREF(slice_tuple);
Py_XDECREF(out_sub); Py_XDECREF(out_sub);
Py_XDECREF(%(out)s); Py_XDECREF(slice_tuple);
%(fail)s; Py_XDECREF(start);
} start = stop;
Py_XDECREF(full_slice);
Py_XDECREF(section_slice);
Py_XDECREF(out_sub);
Py_XDECREF(slice_tuple);
start = stop;
""" % locals() """ % locals()
str+=""" str+="""
Py_XDECREF(start); Py_XDECREF(start);
Py_XDECREF(stop); Py_XDECREF(stop);
Py_XDECREF(step);""" Py_XDECREF(step);"""
return str return str
gpu_join = GpuJoin() gpu_join = GpuJoin()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论