Select elements from input along dim at the given indices. Similar to gather but for general index selection patterns.