initial commit
[jalview.git] / forester / archive / RIO / others / hmmer / src / threads.c
1 /************************************************************
2  * HMMER - Biological sequence analysis with profile HMMs
3  * Copyright (C) 1992-1999 Washington University School of Medicine
4  * All Rights Reserved
5  * 
6  *     This source code is distributed under the terms of the
7  *     GNU General Public License. See the files COPYING and LICENSE
8  *     for details.
9  ************************************************************/
10
11 /* threads.c
12  * SRE, Fri Jul 10 10:05:44 1998
13  * 
14  * Pthreads code shared by hmmsearch, hmmcalibrate, and hmmpfam
15  * to coarse-grain parallelize on platforms capable of POSIX
16  * threads. Most of the threads code, however, is in the respective
17  * main's, i.e. hmmsearch.c, hmmpfam.c, hmmcalibrate.c
18  * 
19  * RCS $Id: threads.c,v 1.1.1.1 2005/03/22 08:34:02 cmzmasek Exp $
20  */
21
22 #ifdef HMMER_THREADS            /* conditional inclusion of the entire file */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <float.h>
27 #include <pthread.h>
28
29 #include "structs.h"
30 #include "funcs.h"
31 #include "squid.h"
32 #include "sqfuncs.h"
33
34
35 /* Function: ThreadNumber()
36  * Date:     SRE, Sat Jul 11 11:03:50 1998 [St. Louis]
37  *
38  * Purpose:  Recommend how many threads to use. 
39  *
40  *             - if we can determine the number of processors
41  *               on the machine by SQD_NPROC, use that. This
42  *               should succeed for SGI IRIX, Digital UNIX, and 
43  *               Sun Solaris platforms.
44  *             - if not, assume two processors. We're probably
45  *               on a FreeBSD or Linux box, and odds are that its
46  *               a dualprocessor.
47  *             - if HMMER_NCPU is defined in config.h, use that
48  *               number instead; allows Linux or FreeBSD machines
49  *               to compile code for a quadprocessor, for instance.
50  *               That define can be overridden at compile
51  *               time by a -DHMMER_NCPU=x, where x is the
52  *               number of threads..
53  *             - if HMMER_NCPU is defined in the environment,
54  *               use that number, overriding all others.
55  *
56  *           Typically, we'll set the default number of
57  *           threads with ThreadNumber() but allow it
58  *           to be overridden at the command line with --cpu.    
59  *           
60  *           Summarizing priority:
61  *                  --ncpu <x> option
62  *                  environment variable, setenv HMMER_NCPU x
63  *                  compile-time, MDEFS=HMMER_NCPU=x
64  *                  compile-time, config.h definition of HMMER_NCPU
65  *                  SQD_NPROC, or 2 if SQD_NPROC doesn't work.
66  *
67  * Args:     void
68  *
69  * Returns:  >= 1, recommended number of threads
70  */
71 int
72 ThreadNumber(void)
73 {
74   int   num;
75   char *env;
76
77   num = SQD_NPROC;              /* SGI, Sun, Digital: get # of available CPUs */
78   if (num == -1) num = 2;       /* Linux, FreeBSD: assume dualprocessor       */
79 #ifdef HMMER_NCPU       
80   num = HMMER_NCPU;             /* allow config.h to override; usually we don't */
81 #endif
82                                 /* allow environment variable to override */
83   if ((env = getenv("HMMER_NCPU")) != NULL)
84     num = atoi(env);            
85   if (num <= 0) num = 1;        /* silent sanity check */
86   SQD_DPRINTF1(("ThreadNumber(): setting number of threads to %d\n", num));
87   return num;
88 }
89
90 #endif /*HMMER_THREADS*/