From 59d0ada570e130957ea1528e9c300f0085f62bd8 Mon Sep 17 00:00:00 2001
From: magnum <magnum>
Date: Wed, 30 Nov 2011 14:36:38 +0100
Subject: [PATCH 27/29] crc32, cap OMP num threads at 4 and dynamically
 allocate. bench.c, report & reset capping of OMP
 threads

---
 src/bench.c          |   39 ++++++++++++++++++++++++---------------
 src/crc32_fmt_plug.c |   17 +++++++++++++----
 2 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/src/bench.c b/src/bench.c
index 5004a8a..f65a3bb 100644
--- a/src/bench.c
+++ b/src/bench.c
@@ -325,6 +325,13 @@ int benchmark_all(void)
 	struct bench_results results_1, results_m;
 	char s_real[64], s_virtual[64];
 	unsigned int total, failed;
+#ifdef _OPENMP
+	int ompt;
+	int ompt_start = omp_get_max_threads();
+#ifdef HAVE_MPI
+	static int haveWarned = 0;
+#endif
+#endif
 
 	if (!benchmark_time)
 		puts("Warning: doing quick benchmarking - "
@@ -359,21 +366,28 @@ int benchmark_all(void)
 #endif
 
 #if defined(HAVE_MPI) && defined(_OPENMP)
-		static int haveWarned = 0;
-		int ompt = omp_get_max_threads();
 		if (format->params.flags & FMT_OMP &&
-		    ompt > 1 && mpi_p > 1 && haveWarned++ == 0) {
+		    ompt_start > 1 && mpi_p > 1 && haveWarned++ == 0) {
 			if(cfg_get_bool(SECTION_OPTIONS, NULL, "MPIOMPmutex", 1)) {
+				omp_set_num_threads(1);
+				ompt_start = 1;
 				if(cfg_get_bool(SECTION_OPTIONS, NULL, "MPIOMPverbose", 1) &&
-				   mpi_id == 0)
+				   mpi_id == 0) {
 					printf("MPI in use, disabling OMP (see doc/README.mpi)\n\n");
-			} else
+				}
+			} else {
 				if(cfg_get_bool(SECTION_OPTIONS, NULL, "MPIOMPverbose", 1) &&
-				   mpi_id == 0)
+				   mpi_id == 0) {
 					printf("Note: Running both MPI and OMP (see doc/README.mpi)\n\n");
+				}
+			}
 		}
 #endif
 		fmt_init(format);
+#ifdef _OPENMP
+		// format's init() or MPIOMPmutex may have capped the number of threads
+		ompt = omp_get_max_threads();
+#endif /* _OPENMP */
 
 		printf("Benchmarking: %s%s [%s]%s... ",
 			format->params.format_name,
@@ -387,14 +401,6 @@ int benchmark_all(void)
 		fflush(stdout);
 
 #ifdef HAVE_MPI
-#ifdef _OPENMP
-		if (format->params.flags & FMT_OMP &&
-		    omp_get_max_threads() > 1 && mpi_p > 1)
-			if(cfg_get_bool(SECTION_OPTIONS, NULL, "MPIOMPmutex", 1)) {
-				omp_set_num_threads(1);
-				ompt = 1;
-			}
-#endif /* _OPENMP */
 		if (mpi_p > 1) {
 			printf("(%uxMPI", mpi_p);
 #ifdef _OPENMP
@@ -413,7 +419,6 @@ int benchmark_all(void)
 		fflush(stdout);
 #else /* HAVE_MPI */
 #ifdef _OPENMP
-		int ompt = omp_get_max_threads();
 		if (format->params.flags & FMT_OMP && ompt > 1)
 			printf("(%dxOMP) ", ompt);
 		fflush(stdout);
@@ -455,6 +460,10 @@ int benchmark_all(void)
 		}
 
 		puts("DONE");
+#ifdef _OPENMP
+		// reset this in case format capped it (we may be running more formats)
+		omp_set_num_threads(ompt_start);
+#endif
 
 #ifdef HAVE_MPI
 		if (mpi_p > 1) {
diff --git a/src/crc32_fmt_plug.c b/src/crc32_fmt_plug.c
index 0a311bb..05fbb49 100644
--- a/src/crc32_fmt_plug.c
+++ b/src/crc32_fmt_plug.c
@@ -46,7 +46,7 @@
 #define SALT_SIZE			4
 
 #define MIN_KEYS_PER_CRYPT		1
-#define MAX_KEYS_PER_CRYPT		16384
+#define MAX_KEYS_PER_CRYPT		8192 // per thread
 
 static struct fmt_tests tests[] = {
 	{"$crc32$00000000.fa455f6b", "ripper"},
@@ -58,13 +58,22 @@ static struct fmt_tests tests[] = {
 };
 
 static char (*saved_key)[PLAINTEXT_LENGTH + 1];
-static ARCH_WORD_32 crcs[MAX_KEYS_PER_CRYPT];
+static ARCH_WORD_32 (*crcs);
 static ARCH_WORD_32 crcsalt;
 
 static void init(struct fmt_main *pFmt)
 {
-	saved_key = mem_alloc_tiny(sizeof(*saved_key) * pFmt->params.max_keys_per_crypt, MEM_ALIGN_NONE);
-	memset(saved_key, 0, (sizeof(*saved_key) * pFmt->params.max_keys_per_crypt));
+#ifdef _OPENMP
+	int n = omp_get_max_threads();
+	if (n > 4) {
+		n = 4; // it just won't scale further
+		omp_set_num_threads(n);
+	}
+	pFmt->params.max_keys_per_crypt = MAX_KEYS_PER_CRYPT * n;
+#endif
+	//printf("Using %u x %u = %u keys per crypt\n", MAX_KEYS_PER_CRYPT, n, pFmt->params.max_keys_per_crypt);
+	saved_key = mem_calloc_tiny(sizeof(*saved_key) * pFmt->params.max_keys_per_crypt, MEM_ALIGN_NONE);
+	crcs = mem_calloc_tiny(sizeof(*crcs) * pFmt->params.max_keys_per_crypt, MEM_ALIGN_WORD);
 }
 
 static int valid(char *ciphertext, struct fmt_main *pFmt)
-- 
1.7.5.4

