safely compute gridsize