1 /*     CalculiX - A 3-dimensional finite element program                 */
2 /*              Copyright (C) 1998-2021 Guido Dhondt                          */
3 
4 /*     This program is free software; you can redistribute it and/or     */
5 /*     modify it under the terms of the GNU General Public License as    */
6 /*     published by the Free Software Foundation(version 2);    */
7 /*                    */
8 
9 /*     This program is distributed in the hope that it will be useful,   */
10 /*     but WITHOUT ANY WARRANTY; without even the implied warranty of    */
11 /*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
12 /*     GNU General Public License for more details.                      */
13 
14 /*     You should have received a copy of the GNU General Public License */
15 /*     along with this program; if not, write to the Free Software       */
16 /*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         */
17 
18 /*     A parallel copy of arrays which depent on active element 	 */
19 
20 
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <stdlib.h>
25 #include <pthread.h>
26 #include "CalculiX.h"
27 
28 static ITG *neapar=NULL,*nebpar=NULL;
29 
30 static double *b1,*scal11,*fext1,*f1,*alpha1,*fextini1,*fini1,
31     *adb1,*aux21;
32 
res2parll(double * b,double * scal1,double * fext,double * f,double * alpha,double * fextini,double * fini,double * adb,double * aux2,ITG * neq0,ITG * num_cpus)33 void res2parll(double *b,double *scal1,double *fext,double *f,
34 		    double *alpha,double *fextini,double *fini,
35 		    double *adb,double *aux2,ITG *neq0,ITG *num_cpus){
36 
37     ITG i,idelta,isum;
38 
39     /* variables for multithreading procedure */
40 
41     ITG *ithread=NULL;
42 
43     pthread_t tid[*num_cpus];
44 
45     /* determining the element bounds in each thread */
46 
47     NNEW(neapar,ITG,*num_cpus);
48     NNEW(nebpar,ITG,*num_cpus);
49 
50     /* dividing the element number range into num_cpus equal numbers of
51        active entries.  */
52 
53     idelta=(ITG)floor(*neq0/(double)(*num_cpus));
54     isum=0;
55     for(i=0;i<*num_cpus;i++){
56 	neapar[i]=isum;
57 	if(i!=*num_cpus-1){
58 	    isum+=idelta;
59 	}else{
60 	    isum=*neq0;
61 	}
62 	nebpar[i]=isum;
63     }
64 
65     /* create threads and wait */
66 
67     b1=b;scal11=scal1;fext1=fext;f1=f;alpha1=alpha;fextini1=fextini;
68     fini1=fini;adb1=adb;aux21=aux2;
69 
70     NNEW(ithread,ITG,*num_cpus);
71 
72     for(i=0; i<*num_cpus; i++)  {
73       ithread[i]=i;
74       pthread_create(&tid[i], NULL, (void *)res2parllmt, (void *)&ithread[i]);
75     }
76     for(i=0; i<*num_cpus; i++)  pthread_join(tid[i], NULL);
77 
78     SFREE(ithread);SFREE(neapar);SFREE(nebpar);
79 
80 }
81 
82 /* subroutine for multithreading of res2parll */
83 
res2parllmt(ITG * i)84 void *res2parllmt(ITG *i){
85 
86     ITG nea,neb,k;
87 
88     nea=neapar[*i];
89     neb=nebpar[*i];
90 
91     for(k=nea;k<neb;++k){
92 	b1[k]=*scal11*(fext1[k]-f1[k])-alpha1[0]*(fextini1[k]-fini1[k])
93 	    -adb1[k]*aux21[k];
94     }
95 
96     return NULL;
97 }
98