Rack one's brains
(See: Beat one's brains out)
Origin
“Rack” is the name of the medieval instrument of torture on which
the victim's body was stretched until it could literally be torn apart.
No other method of torture was more effective, consequently "to rack
your brains" in search of a solution is a form of mental torture