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,*mt1,*nactdof1;
29 
30 static double *aux21,*veold1;
31 
res3parll(ITG * mt,ITG * nactdof,double * aux2,double * veold,ITG * nk,ITG * num_cpus)32 void res3parll(ITG *mt,ITG *nactdof,double *aux2,double *veold,
33 		    ITG *nk,ITG *num_cpus){
34 
35     ITG i,idelta,isum;
36 
37     /* variables for multithreading procedure */
38 
39     ITG *ithread=NULL;
40 
41     pthread_t tid[*num_cpus];
42 
43     /* determining the element bounds in each thread */
44 
45     NNEW(neapar,ITG,*num_cpus);
46     NNEW(nebpar,ITG,*num_cpus);
47 
48     /* dividing the element number range into num_cpus equal numbers of
49        active entries.  */
50 
51     idelta=(ITG)floor(*nk/(double)(*num_cpus));
52     isum=0;
53     for(i=0;i<*num_cpus;i++){
54 	neapar[i]=isum;
55 	if(i!=*num_cpus-1){
56 	    isum+=idelta;
57 	}else{
58 	    isum=*nk;
59 	}
60 	nebpar[i]=isum;
61     }
62 
63     /* create threads and wait */
64 
65     mt1=mt;nactdof1=nactdof;aux21=aux2;veold1=veold;
66 
67     NNEW(ithread,ITG,*num_cpus);
68 
69     for(i=0; i<*num_cpus; i++)  {
70       ithread[i]=i;
71       pthread_create(&tid[i], NULL, (void *)res3parllmt, (void *)&ithread[i]);
72     }
73     for(i=0; i<*num_cpus; i++)  pthread_join(tid[i], NULL);
74 
75     SFREE(ithread);SFREE(neapar);SFREE(nebpar);
76 
77 }
78 
79 /* subroutine for multithreading of res3parll */
80 
res3parllmt(ITG * i)81 void *res3parllmt(ITG *i){
82 
83     ITG nea,neb,k,j;
84 
85     nea=neapar[*i];
86     neb=nebpar[*i];
87 
88     for(k=nea;k<neb;++k){
89 	if(nactdof1[*mt1*k]>0){aux21[nactdof1[*mt1*k]-1]=0.;}
90 	for(j=1;j<*mt1;++j){
91 	    if(nactdof1[*mt1*k+j]>0){
92 		aux21[nactdof1[*mt1*k+j]-1]=veold1[*mt1*k+j];}
93 	}
94     }
95 
96     return NULL;
97 }
98