Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
samdporter opened this issue Dec 3, 2024 · 0 comments

Comments

@samdporter
Copy link
Contributor

cSTIR_computeObjectiveFunctionGradient(void* ptr_f, void* ptr_i, int subset, void* ptr_g)

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant