Thread safe version for openMP. Added test code both in openMP and MPI.