In the second last for loop of initGlobalAlign function,

nextImage->r = am[pid][id].r * nodes[pid].r ;

should replace

nextImage->r = nodes[pid].r * am[pid][id].r ;