1

This C program calls METIS to partition a mesh.

Edit: New version of the C program taking into account comments of WeatherVane and PaulOgilvie.

On my GNU/Linux I get the results:

objval: 14
epart: 0 0 0 0 0 1 2 2 1 0 0 1 2 2 1 2 2 1 
npart: 0 0 0 2 0 0 1 1 2 2 2 1 2 2 1 1 
8

while on my OSX I get:

objval: 17
epart: 0 1 1 0 1 0 2 2 0 1 1 1 2 2 1 2 2 0 
npart: 0 1 1 1 0 1 0 1 2 2 2 0 2 2 0 0 
8

What causes the results to be different?

How to fix it, I mean, always get the same results whatever the OS/architecture/compiler is?

Note: idx_t is int64_t, which is long on my GNU/Linux, but long long on my OSX.

My GNU/Linux

$ cat /etc/issue
Ubuntu 12.04.4 LTS \n \l

$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ uname -s -r -v -p -i -o
Linux 3.5.0-45-generic #68~precise1-Ubuntu SMP Wed Dec 4 16:18:46 UTC 2013 x86_64 x86_64 GNU/Linux

My OSX

$ sw_vers 

ProductName:    Mac OS X
ProductVersion: 10.9.5
BuildVersion:   13F34

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

$ uname -m -p -r -s -v
Darwin 13.4.0 Darwin Kernel Version 13.4.0: Sun Aug 17 19:50:11 PDT 2014; root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64 i386

METIS installation

METIS version is 5.1.0

I have installed METIS with miniconda.

The packages are here (files linux-64/metis-5.1.0-0.tar.bz2 and osx-64/metis-5.1.0-2.tar.bz2).

These packages have been built with this recipe.

user744629
  • 1,961
  • 1
  • 18
  • 27
  • Read about **[undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior)**, e.g. follow links about it [here](http://programmers.stackexchange.com/a/289020/40065) – Basile Starynkevitch Jul 08 '15 at 17:09
  • Hope I will not have to dig in METIS code to find this kind of bug... (I guess the error is from me). – user744629 Jul 08 '15 at 17:13
  • What about this: *`// Number of nodes in the mesh (15 points). #define NN 16`* so if you have told functions there is one more than there actually is, that could be UB. Or is the comment just a typo? – Weather Vane Jul 08 '15 at 17:29
  • If I read the METIS API doumentation correctly, then certain parameters may be NULL, such as options. However, there is a difference between passing a pointer to an object that is NULL or passing the value NULL. You pass a NULL object whereas METIS might expect the parameter value NULL. e.g.: `METIS_PartMeshNodal(&ne, &nn, ..options,..)` should be `METIS_PartMeshNodal(&ne, &nn, ..NULL,..)`. – Paul Ogilvie Jul 08 '15 at 17:40
  • @WeatherVane good catch, thanks! However the error was in the comment, I just fixed in [here](https://github.com/dfroger/quickstart/commit/4d50b9fb9c2100ee67bce4cd75fa4d7ab86e7202) – user744629 Jul 08 '15 at 19:09
  • @PaulOgilvie Thanks for pointing this! I fixed it [here](https://github.com/dfroger/quickstart/commit/a05c861119970fe72e02605652a5a92034dee5df), but I still get the same different values on GNU/Linux and OSX... – user744629 Jul 08 '15 at 19:20
  • I'm going to debug with GDB and LLDB... – user744629 Jul 08 '15 at 20:08

1 Answers1

1

METIS make use of pseudo-random numbers.

The pseudo-random numbers are generated by GKlib functions. (GKlib is embedded inside METIS tarbarlls).

By default, GKlib uses the rand function from the C standard library, which may generates different number on different platforms. (see: Consistent pseudo-random numbers across platforms).

But GKlib can also be compiled with the flag -DUSE_GKRAND. Instead of using the rand function, it uses its own, which always give the same random numbers of different plateforms.

Compiling with -DUSE_GKRAND the C code in the function give the same results on my GNU/Linux and on my OSX:

objval: 18
epart: 0 0 0 2 1 1 2 2 1 0 0 1 0 1 1 2 2 1 
npart: 0 0 0 0 2 0 1 1 2 1 2 1 2 2 1 1 
8

I've used this conda recipe to build METIS.

Community
  • 1
  • 1
user744629
  • 1,961
  • 1
  • 18
  • 27