/*
    DFT++ is a density functional package developed by the research group
    of Professor Tomas Arias

    Copyright 1996-2003 Sohrab Ismail-Beigi

    This file is part of DFT++.

    DFT++ is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DFT++ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DFT++; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Please see the file CREDITS for a list of authors.

    For academic users, we request that publications using results obtained with
    this software reference

    "New algebraic formulation of density functional calculation," by Sohrab Ismail-Beigi
    and T.A. Arias, Computer Physics Communications 128:1-2, 1-45 (June 2000).

    and, if using the wavelet basis, further reference

    "Multiresolution analysis of electronic structure: semicardinal and wavelet bases,"
    T.A. Arias, Reviews of Modern Physics 71:1, 267-311 (January 1999).

    and 

    "Robust ab initio calculation of condensed matter: transparent convergence through
    semicardinal multiresolution analysis,'' I.P. Daykov, T.A. Arias, and
    Torkel D. Engeness, Physical Review Letters, 90:21, 216402 (May 2003).

    For your convenience, preprints of the above articles may be obtained from
    http://arXiv.org/abs/cond-mat/9909130, 9805262, and 0204411, respectively.
*/

// The routines that launch POSIX threads to handle SMP parallelization.

#ifdef DFT_THREAD

#include "header.h"
#include "parallel.h"

//
// This routine launches System::Get_N_threads() POSIX threads.
// Each thread runs the function "routine" and will gets passed
// i3,i4,p1,p2,p3,p4 verbatim through a dft_thread_data class.
// The integers 0 to N-1 are divided into N_threads batches
// and each thread will get a batch:  i1=start, i2=how many in the batch.
//
void
dft_call_threads(int N,
		 void *p1, void *p2, void *p3, void *p4, void *p5,
		 int i1, int i2, int i3, int i4, int i5, int i6,
		 void *(*routine)(void *))
{
  // If there are too few threads to do N items of work
  int N_threads = System::Get_N_threads();

  static int n_warn = 0;
  if (N < N_threads) {
    if (n_warn < 10 || n_warn%100 == 0) {
	  n_warn++;
	  dft_log("WARNING #%d: N=%d items to work on < N_threads=%d\n",
              n_warn,N,N_threads);
	}
    N_threads = N;
    
// SIB 27/1/00
//       
//        older code just died if there were fewer things to do
//        than the number of threads...
//
//       die("dft_call_threads(): N=%d items to work on < N_threads=%d\n",
// 	  N,N_threads);
//        
//        
  }

  // If there is nothing to do, then just do nothing!!!
  if (N < 1) return;
  
  // N = d*N_threads + r
  int d = N / N_threads;
  int r = N % N_threads;

  // Adjust stacksize of POSIX threads
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setstacksize(&attr,(size_t)System::Get_thread_stacksize());

  // Thread ids
  pthread_t *tid = (pthread_t *)mymalloc(sizeof(pthread_t)*N_threads,
					 "tid","dft_call_threads");

  int t,ierr;
  for (t=0; t < N_threads; t++) {
    // Figure out where this thread should start and how many it should do
    int i_start = t*(d+(t<r)) + r*(t>=r);
    int n_todo  = d + (t<r);

    // To send the thread its data
    dft_thread_data *thd = (dft_thread_data *)
      mymalloc(sizeof(dft_thread_data),"thd","dft_call_threads");
      
    // Fill in the data
    thd->id = t;
    thd->start = i_start;
    thd->end = i_start+n_todo-1;
    thd->n = n_todo;
    thd->i1 = i1;
    thd->i2 = i2;
    thd->i3 = i3;
    thd->i4 = i4;
    thd->i5 = i5;
    thd->i6 = i6;
    thd->p1 = p1;
    thd->p2 = p2;
    thd->p3 = p3;
    thd->p4 = p4;
    thd->p5 = p5;

    // Launch it!
    ierr = pthread_create(&tid[t],&attr,routine,thd);
    if (ierr != 0)
      die("Error %ierr creating thread %d in dft_call_threads\n",ierr,t);
  }
  
  // Wait till the threads are done
  for (t=0; t < N_threads; t++)
    if ((ierr=pthread_join(tid[t],NULL))!=0)
      die("Error %d joining thread %d in Y1^Y2\n",ierr,t);

  myfree(tid);
}

#endif // DFT_THREAD
