Aahz <aahz <at> pythoncraft.com> writes:

> Can you reproduce your problem with stub code that only creates threads?
> If yes, that indicates that you're messing with a thread structure
> somewhere.  Note that because you're using gcc, it's possible there's a
> thread bug on Windows with your tool chain.  You might want to check for
> bug reports.

Here's the simplest piece of code I could think of to reproduce the problem:

---------- primes.cpp ----------
#include <python.h>
#include <omp.h>

using namespace std;

int is_prime(const long n)
{
        long i;

        for (i = 2; i < n; i++)
                if (n % i == 0) return 0;

        return 1;
}

long nb_primes(const long n)
{
        long i, nb = 0;

        #pragma omp parallel for private(i) reduction(+ : nb) // It crashes 
here.
        for (i = 2; i <= n; i++)
                if (is_prime(i)) nb += 1;

        return nb;
}

static PyObject * nb_primes_wrapper(PyObject *self, PyObject *args)
{
        long n, nb;

        if (!PyArg_ParseTuple(args, "l", &n)) return NULL;

        nb = nb_primes(n);

        return Py_BuildValue("l", nb);
}

static PyMethodDef primes_methods[] =
{
        {"nb_primes", nb_primes_wrapper, METH_VARARGS},
        {NULL}
};

PyMODINIT_FUNC initprimes()
{
        PyObject *module;

        module = Py_InitModule("primes", primes_methods);
}
---------- end of primes.cpp ----------

I compile it using:

gcc -c -fopenmp -IC:/Python25/include primes.cpp
gcc -LC:/Python25/libs -LC:/mingw/lib -mdll -o primes.pyd primes.o -lpython25
-lgomp -lpthreadGC2

In Python:

import primes
primes.nb_primes(1000)

works fine, but

import primes
import threading
t = threading.Thread(target = primes.nb_primes, args = (1000,))
t.start()

crashes.

Would anybody be so kind and try to compile it with Microsoft or Intel compiler
and tell me if it also crashes (I tried Visual C++ 2005 Express Edition, but it
does not implement OpenMP). If the bug is with gcc, I will submit the problem to
the appropriate people.

Thank you,

Stéphane Larouche

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to