pub mod tape;
mod error;
#[cfg(feature = "cuda")]
mod ffi;
mod pinned;
pub use error::Error;
pub use pinned::PinnedBuffer;
pub use tape::Document;
#[cfg(feature = "cuda")]
use std::borrow::Cow;
use std::path::Path;
pub const MAX_INPUT_LEN: usize = i32::MAX as usize - 8;
#[derive(Debug, Clone, Copy)]
pub struct LinesOptions {
pub chunk_bytes: usize,
}
impl Default for LinesOptions {
fn default() -> Self {
LinesOptions {
chunk_bytes: 256 * 1024 * 1024,
}
}
}
#[derive(Debug, Clone)]
pub struct CudaInfo {
pub runtime_version: i32,
pub devices: Vec<DeviceInfo>,
pub compiled_archs: String,
pub driver_version: i32,
}
#[derive(Debug, Clone)]
pub struct DeviceInfo {
pub index: i32,
pub name: String,
}
fn check_size(len: usize) -> Result<(), Error> {
if len == 0 {
return Err(Error::EmptyInput);
}
if len >= MAX_INPUT_LEN {
return Err(Error::InputTooLarge {
len,
max: MAX_INPUT_LEN,
});
}
Ok(())
}
#[cfg(feature = "cuda")]
fn single_value(doc: Document<'_>) -> Result<Document<'_>, Error> {
if doc.is_single_value() {
Ok(doc)
} else {
Err(Error::NotSingleValue)
}
}
pub fn parse(input: &[u8]) -> Result<Document<'_>, Error> {
check_size(input.len())?;
#[cfg(feature = "cuda")]
{
let tape = ffi::parse_standard(input)?;
single_value(Document::new(Cow::Borrowed(input), tape))
}
#[cfg(not(feature = "cuda"))]
{
let _ = input;
Err(Error::CudaNotCompiled)
}
}
pub fn parse_owned(input: Vec<u8>) -> Result<Document<'static>, Error> {
check_size(input.len())?;
#[cfg(feature = "cuda")]
{
let tape = ffi::parse_standard(&input)?;
single_value(Document::new(Cow::Owned(input), tape))
}
#[cfg(not(feature = "cuda"))]
{
let _ = input;
Err(Error::CudaNotCompiled)
}
}
pub fn parse_lines(input: &[u8], opts: LinesOptions) -> Result<Document<'_>, Error> {
check_size(input.len())?;
#[cfg(feature = "cuda")]
{
let tape = ffi::parse_lines(input, opts.chunk_bytes)?;
Ok(Document::new(Cow::Borrowed(input), tape))
}
#[cfg(not(feature = "cuda"))]
{
let _ = (input, opts);
Err(Error::CudaNotCompiled)
}
}
pub fn parse_lines_owned(input: Vec<u8>, opts: LinesOptions) -> Result<Document<'static>, Error> {
check_size(input.len())?;
#[cfg(feature = "cuda")]
{
let tape = ffi::parse_lines(&input, opts.chunk_bytes)?;
Ok(Document::new(Cow::Owned(input), tape))
}
#[cfg(not(feature = "cuda"))]
{
let _ = (input, opts);
Err(Error::CudaNotCompiled)
}
}
pub fn parse_file(path: impl AsRef<Path>) -> Result<Document<'static>, Error> {
let bytes = std::fs::read(path)?;
parse_owned(bytes)
}
pub fn set_max_concurrent_parses(n: usize) {
#[cfg(feature = "cuda")]
ffi::set_max_concurrent(n);
#[cfg(not(feature = "cuda"))]
let _ = n;
}
pub fn trim_pinned_cache() {
#[cfg(feature = "cuda")]
unsafe {
cujson_sys::cujson_pinned_cache_trim()
}
}
pub fn cuda_info() -> Result<CudaInfo, Error> {
#[cfg(feature = "cuda")]
{
ffi::cuda_info()
}
#[cfg(not(feature = "cuda"))]
{
Err(Error::CudaNotCompiled)
}
}
pub fn driver_version() -> i32 {
#[cfg(feature = "cuda")]
{
ffi::driver_version()
}
#[cfg(not(feature = "cuda"))]
{
0
}
}
pub fn runtime_version() -> Result<i32, Error> {
#[cfg(feature = "cuda")]
{
ffi::runtime_version()
}
#[cfg(not(feature = "cuda"))]
{
Err(Error::CudaNotCompiled)
}
}
pub fn device_memory() -> Result<(usize, usize), Error> {
#[cfg(feature = "cuda")]
{
ffi::mem_get_info()
}
#[cfg(not(feature = "cuda"))]
{
Err(Error::CudaNotCompiled)
}
}
#[cfg(feature = "cpu-reference")]
pub mod cpu {
use std::borrow::Cow;
pub use crate::tape::Mode;
use crate::tape::{Document, Error, build_tape_cpu};
pub fn parse(input: &[u8], mode: Mode) -> Result<Document<'_>, Error> {
let tape = build_tape_cpu(input, mode)?;
let doc = Document::new(Cow::Borrowed(input), tape);
if mode == Mode::Standard && !doc.is_single_value() {
return Err(Error::NotSingleValue);
}
Ok(doc)
}
}
#[cfg(all(test, not(feature = "cuda")))]
mod tests_no_cuda {
#[test]
fn parse_without_cuda_feature_errors() {
let err = super::parse(b"{}").unwrap_err();
assert!(matches!(err, super::Error::CudaNotCompiled));
}
#[test]
fn cuda_info_without_cuda_feature_errors() {
let err = super::cuda_info().unwrap_err();
assert!(matches!(err, super::Error::CudaNotCompiled));
}
#[test]
fn empty_input_is_rejected_before_cuda_check() {
let err = super::parse(b"").unwrap_err();
assert!(matches!(err, super::Error::EmptyInput));
}
#[test]
fn parse_lines_owned_without_cuda_feature_errors() {
let err =
super::parse_lines_owned(b"{}".to_vec(), super::LinesOptions::default()).unwrap_err();
assert!(matches!(err, super::Error::CudaNotCompiled));
}
}
#[cfg(all(test, feature = "cuda"))]
mod tests_cuda_no_device {
fn gpu_usable() -> bool {
let usable = super::cuda_info().is_ok();
if usable {
eprintln!("skipped: a usable GPU is present");
}
usable
}
#[test]
fn cuda_info_errors_without_a_driver() {
if gpu_usable() {
return;
}
let result = super::cuda_info();
assert!(
result.is_err(),
"expected an Err (no driver/device here), got {result:?}"
);
}
#[test]
fn parse_errors_without_a_driver() {
if gpu_usable() {
return;
}
let result = super::parse(b"{}");
assert!(
result.is_err(),
"expected an Err (no driver/device here), got {}",
result.is_ok()
);
}
}
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;