Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions R/R/bridgestan.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ StanModel <- R6::R6Class("StanModel",
stop("Could not construct model RNG.")
}
private$model <- ptr_out

model_version <- self$model_version()
if (packageVersion("bridgestan") != paste(model_version$major, model_version$minor, model_version$patch, sep = ".")) {
warning(paste0("The version of the compiled model does not match the version of the R library. ",
"Consider recompiling the model."))
}
},
#' @description
#' Get the name of this StanModel.
Expand All @@ -60,6 +66,15 @@ StanModel <- R6::R6Class("StanModel",
PACKAGE = private$lib_name
)$info_out
},

model_version= function() {
.C("bs_version_R",
major = as.integer(0),
minor = as.integer(0),
patch = as.integer(0),
PACKAGE = private$lib_name
)
},
#' @description
#' Return the indexed names of the (constrained) parameters.
#' For containers, indexes are separated by periods (.).
Expand Down
3 changes: 3 additions & 0 deletions c-example/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include <stdio.h>

int main(int argc, char** argv) {
printf("Using BridgeStan version %d.%d.%d\n", bs_major_version,
bs_minor_version, bs_patch_version);

char* data;
if (argc > 1) {
data = argv[1];
Expand Down
12 changes: 12 additions & 0 deletions julia/src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ function model_info(sm::StanModel)
unsafe_string(cstr)
end

"""
model_version(sm)

Return the BridgeStan version of the compiled model `sm`.
"""
function model_version(sm::StanModel)
major = reinterpret(Ptr{Cint}, Libc.Libdl.dlsym(sm.lib, "bs_major_version"))
minor = reinterpret(Ptr{Cint}, Libc.Libdl.dlsym(sm.lib, "bs_minor_version"))
patch = reinterpret(Ptr{Cint}, Libc.Libdl.dlsym(sm.lib, "bs_patch_version"))
(unsafe_load(major), unsafe_load(minor), unsafe_load(patch))
end

"""
param_num(sm; include_tp=false, include_gq=false)

Expand Down
1 change: 1 addition & 0 deletions python/bridgestan/__version.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__version__ = "1.0.2"
__version_info__ = tuple(map(int, __version__.split(".")))
21 changes: 20 additions & 1 deletion python/bridgestan/model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import ctypes
import warnings
from typing import List, Optional, Tuple

import numpy as np
import numpy.typing as npt
from numpy.ctypeslib import ndpointer

from .__version import __version_info__
from .compile import compile_model
from .util import validate_readable

Expand Down Expand Up @@ -56,7 +58,7 @@ def __init__(
model from C++.
"""
validate_readable(model_lib)
if not model_data is None and model_data.endswith('.json'):
if not model_data is None and model_data.endswith(".json"):
validate_readable(model_data)
self.lib_path = model_lib
self.stanlib = ctypes.CDLL(self.lib_path)
Expand All @@ -75,6 +77,13 @@ def __init__(
if not self.model_rng:
raise RuntimeError("could not construct model RNG")

if self.model_version() != __version_info__:
warnings.warn(
"The version of the compiled model does not match the version of the "
"Python package. Consider recompiling the model.",
RuntimeWarning,
)

self._name = self.stanlib.bs_name
self._name.restype = ctypes.c_char_p
self._name.argtypes = [ctypes.c_void_p]
Expand Down Expand Up @@ -227,6 +236,16 @@ def model_info(self) -> str:
"""
return self._model_info(self.model_rng).decode("utf-8")

def model_version(self) -> Tuple[int, int, int]:
"""
Return the BridgeStan version of the compiled model.
"""
return (
ctypes.c_int.in_dll(self.stanlib, "bs_major_version").value,
ctypes.c_int.in_dll(self.stanlib, "bs_minor_version").value,
ctypes.c_int.in_dll(self.stanlib, "bs_patch_version").value,
)

def param_num(self, *, include_tp: bool = False, include_gq: bool = False) -> int:
"""
Return the number of parameters, including transformed
Expand Down
5 changes: 5 additions & 0 deletions src/bridgestan.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "bridgestan.h"
#include "model_rng.cpp"
#include "bridgestanR.cpp"
#include "version.hpp"

int bs_major_version = BRIDGESTAN_MAJOR;
int bs_minor_version = BRIDGESTAN_MINOR;
int bs_patch_version = BRIDGESTAN_PATCH;

bs_model_rng* bs_construct(const char* data_file, unsigned int seed,
unsigned int chain_id) {
Expand Down
5 changes: 5 additions & 0 deletions src/bridgestan.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ extern "C" {
typedef struct bs_model_rng bs_model_rng;
typedef int bool;
#endif

extern int bs_major_version;
extern int bs_minor_version;
extern int bs_patch_version;

/**
* Construct an instance of a model and pseudorandom number
* generator (PRNG) wrapper. Data must be encoded in JSON as
Expand Down
5 changes: 5 additions & 0 deletions src/bridgestanR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
void bs_construct_R(char** data, int* rng, int* chain, bs_model_rng** ptr_out) {
*ptr_out = bs_construct(*data, *rng, *chain);
}
void bs_version_R(int* major, int* minor, int* patch){
*major = bs_major_version;
*minor = bs_minor_version;
*patch = bs_patch_version;
}
void bs_destruct_R(bs_model_rng** model, int* return_code) {
*return_code = bs_destruct(*model);
}
Expand Down
2 changes: 2 additions & 0 deletions src/bridgestanR.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ typedef int bool;
// All calls directly delegated to versions without _R suffix
void bs_construct_R(char** data, int* rng, int* chain, bs_model_rng** ptr_out);

void bs_version_R(int* major, int* minor, int* patch);

void bs_destruct_R(bs_model_rng** model, int* return_code);

void bs_name_R(bs_model_rng** model, char const** name_out);
Expand Down