From bc862c47ef8d581a8f6735c51983d6c9eeb95dfd Mon Sep 17 00:00:00 2001 From: Christoph Niethammer Date: Mon, 10 Feb 2014 18:50:51 +0100 Subject: [PATCH 2/3] Speeding up detection of prime numbers using the fact that the largest prime factor of any number n cannot exceed \sqrt{n}. --- ompi/mpi/c/dims_create.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ompi/mpi/c/dims_create.c b/ompi/mpi/c/dims_create.c index 3d0792f..1c1c381 100644 --- a/ompi/mpi/c/dims_create.c +++ b/ompi/mpi/c/dims_create.c @@ -5,7 +5,7 @@ * Copyright (c) 2004-2005 The University of Tennessee and The University * of Tennessee Research Foundation. All rights * reserved. - * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, + * Copyright (c) 2004-2014 High Performance Computing Center Stuttgart, * University of Stuttgart. All rights reserved. * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. @@ -20,6 +20,8 @@ #include "ompi_config.h" +#include + #include "ompi/mpi/c/bindings.h" #include "ompi/runtime/params.h" #include "ompi/communicator/communicator.h" @@ -293,13 +295,14 @@ getprimes(int num, int *pnprime, int **pprimes) { primes[i++] = 2; for (n = 3; n <= num; n += 2) { - for (j = 1; j < i; ++j) { + double nsqrt = sqrt((double) n); + for(j = 0; (double) primes[j] <= nsqrt; j++) { if ((n % primes[j]) == 0) { break; } } - if (j == i) { + if (primes[j] > nsqrt) { if (i >= size) { return MPI_ERR_DIMS; } -- 1.8.3.2