| 1 |
"""Unit tests for the JIT compiler""" |
| 2 |
|
| 3 |
# Copyright (C) 2011 Anders Logg |
| 4 |
# |
| 5 |
# This file is part of DOLFIN. |
| 6 |
# |
| 7 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# DOLFIN is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import pytest |
| 21 |
import platform |
| 22 |
import dolfin |
| 23 |
from dolfin import * |
| 24 |
from dolfin_utils.test import (skip_if_not_PETSc, skip_if_not_SLEPc, |
| 25 |
skip_if_not_MPI, skip_in_serial, |
| 26 |
skip_if_not_petsc4py) |
| 27 |
|
| 28 |
|
| 29 |
def test_nasty_jit_caching_bug(): |
| 30 |
|
| 31 |
# This may result in something like "matrices are not aligned" |
| 32 |
# from FIAT if the JIT caching does not recognize that the two |
| 33 |
# forms are different |
| 34 |
|
| 35 |
default_parameters = parameters["form_compiler"]["representation"] |
| 36 |
for representation in ["quadrature"]: |
| 37 |
|
| 38 |
parameters["form_compiler"]["representation"] = representation |
| 39 |
|
| 40 |
M1 = assemble(Constant(1.0)*dx(UnitSquareMesh(4, 4))) |
| 41 |
M2 = assemble(Constant(1.0)*dx(UnitCubeMesh(4, 4, 4))) |
| 42 |
|
| 43 |
assert round(M1 - 1.0, 7) == 0 |
| 44 |
assert round(M2 - 1.0, 7) == 0 |
| 45 |
|
| 46 |
parameters["form_compiler"]["representation"] = default_parameters |
| 47 |
|
| 48 |
|
| 49 |
def test_mpi_pybind11(): |
| 50 |
""" |
| 51 |
Test MPICommWrapper <-> mpi4py.MPI.Comm conversion for JIT-ed code |
| 52 |
""" |
| 53 |
cpp_code = """ |
| 54 |
#include <pybind11/pybind11.h> |
| 55 |
#include <dolfin_wrappers/MPICommWrapper.h> |
| 56 |
namespace dolfin |
| 57 |
{ |
| 58 |
dolfin_wrappers::MPICommWrapper |
| 59 |
test_comm_passing(const dolfin_wrappers::MPICommWrapper comm) |
| 60 |
{ |
| 61 |
MPI_Comm c = comm.get(); |
| 62 |
return dolfin_wrappers::MPICommWrapper(c); |
| 63 |
} |
| 64 |
} |
| 65 |
PYBIND11_MODULE(SIGNATURE, m) |
| 66 |
{ |
| 67 |
m.def("test_comm_passing", &dolfin::test_comm_passing); |
| 68 |
} |
| 69 |
""" |
| 70 |
|
| 71 |
# Import MPI_COMM_WORLD |
| 72 |
if dolfin.has_mpi4py(): |
| 73 |
from mpi4py import MPI |
| 74 |
w1 = MPI.COMM_WORLD |
| 75 |
else: |
| 76 |
w1 = dolfin.MPI.comm_world |
| 77 |
|
| 78 |
# Compile the JIT module |
| 79 |
return pytest.xfail('Include path for dolfin_wrappers/* not set up to ' |
| 80 |
'work in the JIT at the moment') |
| 81 |
mod = dolfin.compile_cpp_code(cpp_code) |
| 82 |
|
| 83 |
# Pass a comm into C++ and get a new wrapper of the same comm back |
| 84 |
w2 = mod.test_comm_passing(w1) |
| 85 |
|
| 86 |
if dolfin.has_mpi4py(): |
| 87 |
assert isinstance(w2, MPI.Comm) |
| 88 |
else: |
| 89 |
assert isinstance(w2, dolfin.cpp.MPICommWrapper) |
| 90 |
assert w1.underlying_comm() == w2.underlying_comm() |
| 91 |
|
| 92 |
|
| 93 |
@skip_if_not_PETSc |
| 94 |
def test_petsc(): |
| 95 |
create_matrix_code = r''' |
| 96 |
#include <pybind11/pybind11.h> |
| 97 |
#include <dolfin.h> |
| 98 |
namespace dolfin |
| 99 |
{ |
| 100 |
std::shared_ptr<PETScMatrix> create_matrix(void) { |
| 101 |
Mat I; |
| 102 |
std::shared_ptr<PETScMatrix> ptr = std::make_shared<PETScMatrix>(I); |
| 103 |
return ptr; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
PYBIND11_MODULE(SIGNATURE, m) |
| 108 |
{ |
| 109 |
m.def("create_matrix", &dolfin::create_matrix); |
| 110 |
} |
| 111 |
''' |
| 112 |
module = compile_cpp_code(create_matrix_code) |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.skip |
| 116 |
@skip_if_not_SLEPc |
| 117 |
def test_slepc(): |
| 118 |
create_eps_code = r''' |
| 119 |
#include <pybind11/pybind11.h> |
| 120 |
#include <dolfin.h> |
| 121 |
#include <slepc.h> |
| 122 |
namespace dolfin |
| 123 |
{ |
| 124 |
std::shared_ptr<EPS> create_matrix(MPI_Comm comm) { |
| 125 |
EPS eps; |
| 126 |
EPSCreate(comm, &eps); |
| 127 |
std::shared_ptr<EPS> ptr = std::make_shared<EPS>(eps); |
| 128 |
return ptr; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
PYBIND11_MODULE(SIGNATURE, m) |
| 133 |
{ |
| 134 |
m.def("create_matrix", &dolfin::create_matrix); |
| 135 |
} |
| 136 |
|
| 137 |
''' |
| 138 |
compile_cpp_code(create_eps_code) |
| 139 |
|
| 140 |
|
| 141 |
def test_pass_array_int(): |
| 142 |
import numpy |
| 143 |
|
| 144 |
code = """ |
| 145 |
#include <Eigen/Core> |
| 146 |
#include <pybind11/pybind11.h> |
| 147 |
#include <pybind11/eigen.h> |
| 148 |
using IntVecIn = Eigen::Ref<const Eigen::VectorXi>; |
| 149 |
int test_int_array(const IntVecIn arr) |
| 150 |
{ |
| 151 |
return arr.sum(); |
| 152 |
} |
| 153 |
PYBIND11_MODULE(SIGNATURE, m) |
| 154 |
{ |
| 155 |
m.def("test_int_array", &test_int_array); |
| 156 |
} |
| 157 |
""" |
| 158 |
module = compile_cpp_code(code) |
| 159 |
arr = numpy.array([1, 2, 4, 8], dtype=numpy.intc) |
| 160 |
ans = module.test_int_array(arr) |
| 161 |
assert ans == arr.sum() == 15 |
| 162 |
|
| 163 |
|
| 164 |
def test_pass_array_double(): |
| 165 |
import numpy |
| 166 |
|
| 167 |
code = """ |
| 168 |
#include <Eigen/Core> |
| 169 |
#include <pybind11/pybind11.h> |
| 170 |
#include <pybind11/eigen.h> |
| 171 |
using DoubleVecIn = Eigen::Ref<const Eigen::VectorXd>; |
| 172 |
int test_double_array(const DoubleVecIn arr) |
| 173 |
{ |
| 174 |
return arr.sum(); |
| 175 |
} |
| 176 |
PYBIND11_MODULE(SIGNATURE, m) |
| 177 |
{ |
| 178 |
m.def("test_double_array", &test_double_array); |
| 179 |
} |
| 180 |
""" |
| 181 |
module = compile_cpp_code(code) |
| 182 |
arr = numpy.array([1, 2, 4, 8], dtype=float) |
| 183 |
ans = module.test_double_array(arr) |
| 184 |
assert abs(arr.sum() - 15) < 1e-15 |
| 185 |
assert abs(ans - 15) < 1e-15 |
| 186 |
|
| 187 |
@pytest.mark.skip |
| 188 |
@skip_if_not_PETSc |
| 189 |
def test_compile_extension_module(): |
| 190 |
|
| 191 |
# This test should do basically the same as the docstring of the |
| 192 |
# compile_extension_module function in compilemodule.py. Remember |
| 193 |
# to update the docstring if the test is modified! |
| 194 |
|
| 195 |
from numpy import arange, exp |
| 196 |
code = """ |
| 197 |
#include <pybind11/pybind11.h> |
| 198 |
|
| 199 |
#include <petscvec.h> |
| 200 |
#include <dolfin/la/PETScVector.h> |
| 201 |
|
| 202 |
void PETSc_exp(std::shared_ptr<dolfin::PETScVector> vec) |
| 203 |
{ |
| 204 |
Vec x = vec->vec(); |
| 205 |
assert(x); |
| 206 |
VecExp(x); |
| 207 |
} |
| 208 |
|
| 209 |
PYBIND11_MODULE(SIGNATURE, m) |
| 210 |
{ |
| 211 |
m.def("PETSc_exp", &PETSc_exp); |
| 212 |
} |
| 213 |
""" |
| 214 |
|
| 215 |
ext_module = compile_cpp_code(code) |
| 216 |
|
| 217 |
vec = PETScVector(MPI.comm_world, 10) |
| 218 |
np_vec = vec.get_local() |
| 219 |
np_vec[:] = arange(len(np_vec)) |
| 220 |
vec.set_local(np_vec) |
| 221 |
ext_module.PETSc_exp(vec) |
| 222 |
np_vec[:] = exp(np_vec) |
| 223 |
assert (np_vec == vec.get_local()).all() |
| 224 |
|
| 225 |
|
| 226 |
@pytest.mark.xfail |
| 227 |
def test_compile_extension_module_kwargs(): |
| 228 |
# This test check that instant_kwargs of compile_extension_module |
| 229 |
# are taken into account when computing signature |
| 230 |
m2 = compile_cpp_code('', cppargs='-O2') |
| 231 |
m0 = compile_cpp_code('', cppargs='') |
| 232 |
assert not m2.__file__ == m0.__file__ |
| 233 |
|
| 234 |
|
| 235 |
@pytest.mark.skip |
| 236 |
@skip_if_not_petsc4py |
| 237 |
@skip_in_serial |
| 238 |
def test_mpi_dependent_jiting(): |
| 239 |
# FIXME: Not a proper unit test... |
| 240 |
from dolfin import (Expression, UnitSquareMesh, Function, |
| 241 |
TestFunction, Form, FunctionSpace, dx, CompiledSubDomain, |
| 242 |
SubSystemsManager) |
| 243 |
|
| 244 |
# Init petsc (needed to initalize petsc and slepc collectively on |
| 245 |
# all processes) |
| 246 |
SubSystemsManager.init_petsc() |
| 247 |
|
| 248 |
try: |
| 249 |
import mpi4py.MPI as mpi |
| 250 |
except: |
| 251 |
return |
| 252 |
|
| 253 |
try: |
| 254 |
import petsc4py.PETSc as petsc |
| 255 |
except: |
| 256 |
return |
| 257 |
|
| 258 |
# Set communicator and get process information |
| 259 |
comm = mpi.COMM_WORLD |
| 260 |
group = comm.Get_group() |
| 261 |
size = comm.Get_size() |
| 262 |
|
| 263 |
# Only consider parallel runs |
| 264 |
if size == 1: |
| 265 |
return |
| 266 |
|
| 267 |
rank = comm.Get_rank() |
| 268 |
group_comm_0 = petsc.Comm(comm.Create(group.Incl(range(1)))) |
| 269 |
group_comm_1 = petsc.Comm(comm.Create(group.Incl(range(1, 2)))) |
| 270 |
|
| 271 |
if size > 2: |
| 272 |
group_comm_2 = petsc.Comm(comm.Create(group.Incl(range(2,size)))) |
| 273 |
|
| 274 |
if rank == 0: |
| 275 |
e = Expression("4", mpi_comm=group_comm_0, degree=0) |
| 276 |
|
| 277 |
elif rank == 1: |
| 278 |
e = Expression("5", mpi_comm=group_comm_1, degree=0) |
| 279 |
domain = CompiledSubDomain("on_boundary", mpi_comm=group_comm_1, |
| 280 |
degree=0) |
| 281 |
|
| 282 |
else: |
| 283 |
mesh = UnitSquareMesh(group_comm_2, 2, 2) |
| 284 |
V = FunctionSpace(mesh, "P", 1) |
| 285 |
u = Function(V) |
| 286 |
v = TestFunction(V) |
| 287 |
Form(u*v*dx) |