The geometrical structure of Mg,Cd doped 32-atom supercell of AlN was optimized by adopting the ultra-soft pseudopotential method of total-energy plane wave based on the destiny functional theory (DFT). Cell parameters of both doped and undoped cells were calculated theoretically. Binding energy, partial destiny of states, and electron destiny differences of doped AlN crystals were calculated and discussed in detail. The results revealed that compared with Cd, Mg can provides more states of holes. The p-type conduction was obtained, and the result that Mg was a better P-type doping than Cd is proved.