Energy loss of charged particles moving parallel to a magnesium surface: Ab initio calculations