Skip to content

Commit 2ab71da

Browse files
author
Frederic Perez
committed
more python 3.12 fixes
1 parent 823b09d commit 2ab71da

File tree

6 files changed

+87
-32
lines changed

6 files changed

+87
-32
lines changed

src/ElectroMagn/Laser.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,9 @@ void LaserProfileSeparable::initFields( Params &params, Patch *patch, ElectroMag
453453
double LaserProfileSeparable::getAmplitude( std::vector<double>, double t, int j, int k )
454454
{
455455
double amp;
456-
#pragma omp critical
457-
{
458-
double omega = omega_ * chirpProfile_->valueAt( t );
459-
double phi = ( *phase )( j, k );
460-
amp = timeProfile_->valueAt( t-( phi+delay_phase_ )/omega ) * ( *space_envelope )( j, k ) * sin( omega*t - phi );
461-
}
456+
double omega = omega_ * chirpProfile_->valueAt( t );
457+
double phi = ( *phase )( j, k );
458+
amp = timeProfile_->valueAt( t-( phi+delay_phase_ )/omega ) * ( *space_envelope )( j, k ) * sin( omega*t - phi );
462459
return amp;
463460
}
464461

@@ -562,10 +559,7 @@ double LaserProfileFile::getAmplitude( std::vector<double> pos, double t, int j,
562559
for( unsigned int i=0; i<n; i++ ) {
563560
amp += ( *magnitude )( j, k, i ) * cos( omega[i] * t + ( *phase )( j, k, i ) );
564561
}
565-
#pragma omp critical
566-
{
567-
amp *= extraProfile->valueAt( pos, t );
568-
}
562+
amp *= extraProfile->valueAt( pos, t );
569563
return amp;
570564
}
571565

src/ElectroMagn/Laser.h

-2
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,13 @@ class LaserProfileNonSeparable : public LaserProfile
139139
inline double getAmplitude( std::vector<double> pos, double t, int, int ) override
140140
{
141141
double amp;
142-
#pragma omp critical
143142
amp = spaceAndTimeProfile_->valueAt( pos, t );
144143
return amp;
145144
}
146145

147146
inline std::complex<double> getAmplitudecomplex( std::vector<double> pos, double t, int, int ) override
148147
{
149148
std::complex<double> amp;
150-
#pragma omp critical
151149
amp = spaceAndTimeProfile_->complexValueAt( pos, t );
152150
return amp;
153151
}

src/Ionization/IonizationFromRate.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ void IonizationFromRate::operator()( Particles *particles, unsigned int ipart_mi
3838

3939
#ifdef SMILEI_USE_NUMPY
4040
// Run python to evaluate the ionization rate for each particle
41-
PyArrayObject *ret;
4241
unsigned int npart = ipart_max - ipart_min;
43-
#pragma omp critical
42+
SMILEI_PY_ACQUIRE_GIL
4443
{
4544
ParticleData particleData( npart );
4645
particleData.startAt( ipart_min );
4746
particleData.set( particles );
48-
ret = ( PyArrayObject * )PyObject_CallFunctionObjArgs( ionization_rate_, particleData.get(), NULL );
47+
PyArrayObject *ret = ( PyArrayObject * )PyObject_CallFunctionObjArgs( ionization_rate_, particleData.get(), NULL );
4948
PyTools::checkPyError();
5049
if( ret == NULL ) {
5150
ERROR( "ionization_rate profile has not provided a correct result" );
@@ -58,6 +57,7 @@ void IonizationFromRate::operator()( Particles *particles, unsigned int ipart_mi
5857
}
5958
Py_DECREF( ret );
6059
}
60+
SMILEI_PY_RELEASE_GIL
6161
#endif
6262

6363

src/Patch/VectorPatch.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1170,13 +1170,15 @@ void VectorPatch::finalizeSyncAndBCFields( Params &params, SmileiMPI *smpi, SimW
11701170
}
11711171

11721172
timers.maxwellBC.restart();
1173+
SMILEI_PY_SAVE_MASTER_THREAD
11731174
#pragma omp for schedule(static)
11741175
for( unsigned int ipatch=0 ; ipatch<this->size() ; ipatch++ ) {
11751176
// Applies boundary conditions on B
11761177
if ( (!params.is_spectral) || (params.geometry!= "AMcylindrical") )
11771178
( *this )( ipatch )->EMfields->boundaryConditions( time_dual, ( *this )( ipatch ), simWindow );
11781179

11791180
}
1181+
SMILEI_PY_RESTORE_MASTER_THREAD
11801182
SyncVectorPatch::exchangeForPML( params, (*this), smpi );
11811183

11821184
#pragma omp for schedule(static)
@@ -4963,6 +4965,7 @@ void VectorPatch::dynamicsWithoutTasks( Params &params,
49634965
diag_PartEventTracing = smpi->diagPartEventTracing( time_dual, params.timestep);
49644966
#endif
49654967

4968+
SMILEI_PY_SAVE_MASTER_THREAD
49664969
#pragma omp for schedule(runtime)
49674970
for( unsigned int ipatch=0 ; ipatch<this->size() ; ipatch++ ) {
49684971
( *this )( ipatch )->EMfields->restartRhoJ();
@@ -5019,6 +5022,7 @@ void VectorPatch::dynamicsWithoutTasks( Params &params,
50195022
} // end loop on species
50205023
//MESSAGE("species dynamics");
50215024
} // end loop on patches
5025+
SMILEI_PY_RESTORE_MASTER_THREAD
50225026
}
50235027

50245028
void VectorPatch::ponderomotiveUpdateSusceptibilityAndMomentumWithoutTasks( Params &params,

src/Profiles/Function.cpp

+74-15
Original file line numberDiff line numberDiff line change
@@ -8,127 +8,186 @@ using namespace std;
88
// 1D
99
double Function_Python1D::valueAt( double time )
1010
{
11-
return PyTools::runPyFunction( py_profile, time );
11+
SMILEI_PY_ACQUIRE_GIL
12+
double v = PyTools::runPyFunction( py_profile, time );
13+
SMILEI_PY_RELEASE_GIL
14+
return v;
1215
}
1316
double Function_Python1D::valueAt( vector<double>, double time )
1417
{
15-
return PyTools::runPyFunction( py_profile, time );
18+
SMILEI_PY_ACQUIRE_GIL
19+
double v = PyTools::runPyFunction( py_profile, time );
20+
SMILEI_PY_RELEASE_GIL
21+
return v;
1622
}
1723
double Function_Python1D::valueAt( vector<double> x_cell )
1824
{
19-
return PyTools::runPyFunction( py_profile, x_cell[0] );
25+
SMILEI_PY_ACQUIRE_GIL
26+
double v = PyTools::runPyFunction( py_profile, x_cell[0] );
27+
SMILEI_PY_RELEASE_GIL
28+
return v;
2029
}
2130

2231
// 2D
2332
double Function_Python2D::valueAt( vector<double> x_cell, double time )
2433
{
25-
return PyTools::runPyFunction( py_profile, x_cell[0], time );
34+
SMILEI_PY_ACQUIRE_GIL
35+
double v = PyTools::runPyFunction( py_profile, x_cell[0], time );
36+
SMILEI_PY_RELEASE_GIL
37+
return v;
2638
}
2739
double Function_Python2D::valueAt( vector<double> x_cell )
2840
{
29-
return PyTools::runPyFunction( py_profile, x_cell[0], x_cell[1] );
41+
SMILEI_PY_ACQUIRE_GIL
42+
double v = PyTools::runPyFunction( py_profile, x_cell[0], x_cell[1] );
43+
SMILEI_PY_RELEASE_GIL
44+
return v;
3045
}
3146
// 2D complex
3247
std::complex<double> Function_Python2D::complexValueAt( vector<double> x_cell, double time )
3348
{
34-
return PyTools::runPyFunction<std::complex<double>>( py_profile, x_cell[0], time );
49+
SMILEI_PY_ACQUIRE_GIL
50+
std::complex<double> v = PyTools::runPyFunction<std::complex<double>>( py_profile, x_cell[0], time );
51+
SMILEI_PY_RELEASE_GIL
52+
return v;
3553
}
3654
std::complex<double> Function_Python2D::complexValueAt( vector<double> x_cell )
3755
{
38-
return PyTools::runPyFunction<std::complex<double>>( py_profile, x_cell[0], x_cell[1] );
56+
SMILEI_PY_ACQUIRE_GIL
57+
std::complex<double> v = PyTools::runPyFunction<std::complex<double>>( py_profile, x_cell[0], x_cell[1] );
58+
SMILEI_PY_RELEASE_GIL
59+
return v;
3960
}
4061

4162
// 3D
4263
double Function_Python3D::valueAt( vector<double> x_cell, double time )
4364
{
44-
return PyTools::runPyFunction( py_profile, x_cell[0], x_cell[1], time );
65+
SMILEI_PY_ACQUIRE_GIL
66+
double v = PyTools::runPyFunction( py_profile, x_cell[0], x_cell[1], time );
67+
SMILEI_PY_RELEASE_GIL
68+
return v;
4569
}
4670
double Function_Python3D::valueAt( vector<double> x_cell )
4771
{
48-
return PyTools::runPyFunction( py_profile, x_cell[0], x_cell[1], x_cell[2] );
72+
SMILEI_PY_ACQUIRE_GIL
73+
double v = PyTools::runPyFunction( py_profile, x_cell[0], x_cell[1], x_cell[2] );
74+
SMILEI_PY_RELEASE_GIL
75+
return v;
4976
}
5077
// 3D complex
5178
std::complex<double> Function_Python3D::complexValueAt( vector<double> x_cell, double time )
5279
{
53-
return PyTools::runPyFunction<std::complex<double>>( py_profile, x_cell[0], x_cell[1], time );
80+
SMILEI_PY_ACQUIRE_GIL
81+
std::complex<double> v = PyTools::runPyFunction<std::complex<double>>( py_profile, x_cell[0], x_cell[1], time );
82+
SMILEI_PY_RELEASE_GIL
83+
return v;
5484
}
5585

5686
// 4D
5787
double Function_Python4D::valueAt( vector<double> x_cell, double time )
5888
{
59-
return PyTools::runPyFunction( py_profile, x_cell[0], x_cell[1], x_cell[2], time );
89+
SMILEI_PY_ACQUIRE_GIL
90+
double v = PyTools::runPyFunction( py_profile, x_cell[0], x_cell[1], x_cell[2], time );
91+
SMILEI_PY_RELEASE_GIL
92+
return v;
6093
}
6194
// 4D complex
6295
std::complex<double> Function_Python4D::complexValueAt( vector<double> x_cell, double time )
6396
{
64-
return PyTools::runPyFunction<std::complex<double>>( py_profile, x_cell[0], x_cell[1], x_cell[2], time );
97+
SMILEI_PY_ACQUIRE_GIL
98+
std::complex<double> v = PyTools::runPyFunction<std::complex<double>>( py_profile, x_cell[0], x_cell[1], x_cell[2], time );
99+
SMILEI_PY_RELEASE_GIL
100+
return v;
65101
}
66102

67103
// Special cases for locations specified in numpy arrays
68104
#ifdef SMILEI_USE_NUMPY
69105
PyArrayObject *Function_Python1D::valueAt( std::vector<PyArrayObject *> x )
70106
{
71-
return ( PyArrayObject * )PyObject_CallFunctionObjArgs( py_profile, x[0], NULL );
107+
SMILEI_PY_ACQUIRE_GIL
108+
PyArrayObject * v = ( PyArrayObject * )PyObject_CallFunctionObjArgs( py_profile, x[0], NULL );
109+
SMILEI_PY_RELEASE_GIL
110+
return v;
72111
}
73112
PyArrayObject *Function_Python2D::valueAt( std::vector<PyArrayObject *> x )
74113
{
75-
return ( PyArrayObject * )PyObject_CallFunctionObjArgs( py_profile, x[0], x[1], NULL );
114+
SMILEI_PY_ACQUIRE_GIL
115+
PyArrayObject * v = ( PyArrayObject * )PyObject_CallFunctionObjArgs( py_profile, x[0], x[1], NULL );
116+
SMILEI_PY_RELEASE_GIL
117+
return v;
76118
}
77119
PyArrayObject *Function_Python3D::valueAt( std::vector<PyArrayObject *> x )
78120
{
79-
return ( PyArrayObject * )PyObject_CallFunctionObjArgs( py_profile, x[0], x[1], x[2], NULL );
121+
SMILEI_PY_ACQUIRE_GIL
122+
PyArrayObject * v = ( PyArrayObject * )PyObject_CallFunctionObjArgs( py_profile, x[0], x[1], x[2], NULL );
123+
SMILEI_PY_RELEASE_GIL
124+
return v;
80125
}
81126
PyArrayObject *Function_Python2D::complexValueAt( std::vector<PyArrayObject *> x )
82127
{
128+
SMILEI_PY_ACQUIRE_GIL
83129
PyObject *values = PyObject_CallFunctionObjArgs( py_profile, x[0], x[1], NULL );
84130
PyArrayObject *cvalues = ( PyArrayObject * )PyObject_CallMethod( values, const_cast<char *>("astype"), const_cast<char *>("s"), const_cast<char *>("complex"), NULL );
85131
Py_DECREF( values );
132+
SMILEI_PY_RELEASE_GIL
86133
return cvalues;
87134
}
88135

89136
// Time dependent
90137

91138
PyArrayObject *Function_Python1D::valueAt( std::vector<PyArrayObject *>, double time )
92139
{
140+
SMILEI_PY_ACQUIRE_GIL
93141
PyObject *t = PyFloat_FromDouble( time );
94142
PyArrayObject * ret = ( PyArrayObject * )PyObject_CallFunctionObjArgs( py_profile, t, NULL );
95143
Py_DECREF( t );
144+
SMILEI_PY_RELEASE_GIL
96145
return ret;
97146
}
98147
PyArrayObject *Function_Python2D::valueAt( std::vector<PyArrayObject *> x, double time )
99148
{
149+
SMILEI_PY_ACQUIRE_GIL
100150
PyObject *t = PyFloat_FromDouble( time );
101151
PyArrayObject * ret = ( PyArrayObject * )PyObject_CallFunctionObjArgs( py_profile, x[0], t, NULL );
102152
Py_DECREF( t );
153+
SMILEI_PY_RELEASE_GIL
103154
return ret;
104155
}
105156
PyArrayObject *Function_Python3D::valueAt( std::vector<PyArrayObject *> x, double time )
106157
{
158+
SMILEI_PY_ACQUIRE_GIL
107159
PyObject *t = PyFloat_FromDouble( time );
108160
PyArrayObject * ret = ( PyArrayObject * )PyObject_CallFunctionObjArgs( py_profile, x[0], x[1], t, NULL );
109161
Py_DECREF( t );
162+
SMILEI_PY_RELEASE_GIL
110163
return ret;
111164
}
112165
PyArrayObject *Function_Python4D::valueAt( std::vector<PyArrayObject *> x, double time )
113166
{
167+
SMILEI_PY_ACQUIRE_GIL
114168
PyObject *t = PyFloat_FromDouble( time );
115169
PyArrayObject * ret = ( PyArrayObject * )PyObject_CallFunctionObjArgs( py_profile, x[0], x[1], x[2], t, NULL );
116170
Py_DECREF( t );
171+
SMILEI_PY_RELEASE_GIL
117172
return ret;
118173
}PyArrayObject *Function_Python4D::complexValueAt( std::vector<PyArrayObject *> x, double time )
119174
{
175+
SMILEI_PY_ACQUIRE_GIL
120176
PyObject *t = PyFloat_FromDouble( time );
121177
PyObject * values = PyObject_CallFunctionObjArgs( py_profile, x[0], x[1], x[2], t, NULL );
122178
Py_DECREF( t );
123179
PyArrayObject *cvalues = ( PyArrayObject * )PyObject_CallMethod( values, const_cast<char *>("astype"), const_cast<char *>("s"), const_cast<char *>("complex"), NULL );
124180
Py_DECREF( values );
181+
SMILEI_PY_RELEASE_GIL
125182
return cvalues;
126183
}
127184
PyArrayObject *Function_Python4D::complexValueAt( std::vector<PyArrayObject *> x, PyArrayObject *t )
128185
{
186+
SMILEI_PY_ACQUIRE_GIL
129187
PyObject *values = PyObject_CallFunctionObjArgs( py_profile, x[0], x[1], x[2], t, NULL );
130188
PyArrayObject *cvalues = ( PyArrayObject * )PyObject_CallMethod( values, const_cast<char *>("astype"), const_cast<char *>("s"), const_cast<char *>("complex"), NULL );
131189
Py_DECREF( values );
190+
SMILEI_PY_RELEASE_GIL
132191
return cvalues;
133192
}
134193

src/Tools/PyTools.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
// }
4545
// SMILEI_PY_RESTORE_MASTER_THREAD
4646
#if PY_MAJOR_VERSION > 2 && PY_MINOR_VERSION > 11
47-
#define SMILEI_PY_SAVE_MASTER_THREAD PyThreadState *_save = NULL; _Pragma("omp master") if( Py_IsInitialized() ) _save = PyEval_SaveThread();
48-
#define SMILEI_PY_RESTORE_MASTER_THREAD _Pragma("omp master") if( Py_IsInitialized() ) PyEval_RestoreThread( _save );
47+
#define SMILEI_PY_SAVE_MASTER_THREAD PyThreadState *_save = NULL; _Pragma("omp master") if( Py_IsInitialized() ) _save = PyEval_SaveThread(); _Pragma("omp barrier")
48+
#define SMILEI_PY_RESTORE_MASTER_THREAD _Pragma("omp master") if( Py_IsInitialized() ) PyEval_RestoreThread( _save ); _Pragma("omp barrier")
4949
#define SMILEI_PY_ACQUIRE_GIL PyGILState_STATE _state = PyGILState_Ensure();
5050
#define SMILEI_PY_RELEASE_GIL PyGILState_Release( _state );
5151
#else

0 commit comments

Comments
 (0)