SIRF icon indicating copy to clipboard operation
SIRF copied to clipboard

ObjectiveFunction gradient incorrect when passed the same object for input argument and out

Open samdporter opened this issue 1 year ago • 0 comments

https://github.com/SyneRBI/SIRF/blob/b2053845e25f87149d1a7406846110e4bf66817e/src/xSTIR/cSTIR/cstir.cpp#L1212

ObjectiveFunction gradient incorrect when passed the same object for input argument and out Fails when used with CIL: obj_fun.gradient(x, out=x)

suggestion for fix:

extern "C"
void*
cSTIR_computeObjectiveFunctionGradient(void* ptr_f, void* ptr_i, int subset, void* ptr_g)
{
	try {
		xSTIR_ObjFun3DF& fun = objectFromHandle<xSTIR_ObjFun3DF>(ptr_f);
		STIRImageData& id = objectFromHandle<STIRImageData>(ptr_i);
		STIRImageData& gd = objectFromHandle<STIRImageData>(ptr_g);
		if (&gd == &id)
		{
		    // unsafe to call fun.compute_gradient() with the same object for input and output, so make a copy
			stir::shared_ptr<STIRImageData> id_clone(id.clone());
			auto new_hdl = newObjectHandle(id_clone);
			return cSTIR_computeObjectiveFunctionGradient(ptr_f, new_hdl, subset, ptr_g);
		}
		fun.compute_gradient(id, subset, gd);
		return (void*) new DataHandle;
	}
	CATCH;
}

samdporter avatar Dec 03 '24 18:12 samdporter