Wolframコンパイラで外部ライブラリを呼び出す
はじめに
コンパイル型言語(C,C++,Rust,Swift,Haskell等)の多くはC互換性の動的ライブラリをコンパイルすることができる.これらのライブラリの関数はコンパイルされたWolfram言語で直接呼び出すことができるため,トップレベルのWolfram言語と動的ライブラリの間の高性能リンクを書くことが可能になる.
このチュートリアルには,単独のOpenSSL関数への接続の短い例の他,大規模なSQLite機能のインターフェースの拡張された例が含まれている.
OpenSSLとの接続
OpenSSLは暗号化機能を実装するC互換ライブラリを中心としている.この例ではOpenSSLの暗号論的疑似乱数生成器に接続する.
OpenSSLは以下の型のシグネチャを使ってRAND_bytesという関数を公開する:
int RAND_bytes(unsigned char *buf, int num);
第1引数のbufは,それが乱数のバイトを書き込むバッファである.第2引数のnumはbufに書き込むバイト数である.
dec = LibraryFunctionDeclaration["RAND_bytes", {"CArray"::["CChar"], "CInt"} -> "CInt"];(OpenSSLはWolfram言語カーネルによってデフォルトでロードされているため,第2引数でライブラリを指定する必要がなかった.)
cf = FunctionCompile[dec,
Function[Typed[len, "CInt"], Module[{buf},
buf = CreateTypeInstance["Managed"::["CArray"::["CChar"]], len];
LibraryFunction["RAND_bytes"][buf, len];
CreateTypeInstance["NumericArray", buf, len]
]]
]まず,指定された長さで管理される"CArray"を作成するためにCreateTypeInstanceが使われる.これはランダムなバイトが書き込まれるバッファである.
次にLibraryFunctionDeclarationによって宣言されたOpenSSL関数を呼び出すためにLibraryFunction["RAND_bytes"]が使われる.
最後にCreateTypeInstanceを再び使ってバッファの要素を抽出しそれらを"NumericArray"にコピーする.このタスクにはFromRawPointerを使うこともできる.
Normal@cf[10]SQLiteとの接続
SQLiteはSQLデータベースを実装するC互換ライブラリである.このチュートリアルを実行するためにはSQLiteがパクレットとしてダウンロードされていなければならない.
PacletInstall["LibSQLite"]パクレットはPacletUninstallを実行するとアンインストールすることができる.
SQLiteの文書化されたCインターフェースには以下のシグネチャを持つsqlite3_libversion_numberという関数が含まれている:
int sqlite3_libversion_number(void);
versionDec = LibraryFunctionDeclaration["sqlite3_libversion_number", "libsqlite3", {} -> "CInt"];cf = FunctionCompile[versionDec, Function[{},
LibraryFunction["sqlite3_libversion_number"][]
]]cf[]これは単独の関数の呼出しを示しているが,同じメカニズムを使って複雑なカスタムデータ構造を動的ライブラリの間のやり取りをする複雑なリンクを作成することができる.
このチュートリアルはトップレベルのWolfram言語とSQLiteの間の効率的なリンクを作成するのに必要なステップを説明する.まずSQLiteデータベースを開き,自動的に閉じるためのシステムを作成する.それからこれらのデータベースでクエリを実行する.最後にクエリから結果を読み戻し,その後のデータ解析に使えるようにそれをWolfram言語の式に変換する.
データベースを開き閉じる
SQLiteデータベースに接続するための第一歩は,それを開き閉じることである.これは2つの関数で行うことができ,以下のシグネチャを持つ:
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
int sqlite3_close(sqlite3*);
openCloseDecs = {
TypeDeclaration["Alias", "SQLiteDB", "OpaqueRawPointer"],
LibraryFunctionDeclaration["sqlite3_open", "libsqlite3", {"CString", "RawPointer"::["SQLiteDB"]} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_close", "libsqlite3", {"SQLiteDB"} -> "CInt"]
};openClose = FunctionCompile[openCloseDecs,
Function[Typed[path, "String"],
Module[{cstrPath, dbPointer, db}, cstrPath = Cast[path, "Managed"::["CString"]];
dbPointer = Typed[ToRawPointer[], "RawPointer"::["SQLiteDB"]];
LibraryFunction["sqlite3_open"][cstrPath, dbPointer];
db = FromRawPointer[dbPointer];
LibraryFunction["sqlite3_close"][db]
]
]
]openClose[FindFile["ExampleData/ecommerce-database.sqlite"]]openDB = FunctionCompile[openCloseDecs,
Function[Typed[path, "String"],
Module[{cstrPath, dbPointer},
cstrPath = Cast[path, "Managed"::["CString"]];
dbPointer = Typed[ToRawPointer[], "RawPointer"::["SQLiteDB"]];
LibraryFunction["sqlite3_open"][cstrPath, dbPointer];
CreateTypeInstance["Managed", FromRawPointer[dbPointer], LibraryFunction["sqlite3_close"][#];&]
]
]
]openDB[FindFile["ExampleData/ecommerce-database.sqlite"]]この"Managed"オブジェクトにはコンパイルされたコードとトップレベルのコードの両方における共有参照カウントがあり,その参照カウントが0になると解放コードが実行される.つまりトップレベルでSQLiteデータベースオブジェクトを送り合ってもメモリ的に安全ということである.
簡単なクエリ
データベースを開いたり(自動的に)閉じたりできるようになったので,クエリを実行してみる.SQLiteの最も簡単なクエリインターフェースはsqlite3_execを介したものである:
int sqlite3_exec(
sqlite3*, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
execDecs = Join[
openCloseDecs, {
LibraryFunctionDeclaration["sqlite3_exec", "libsqlite3", {
"SQLiteDB",
"CString",
"OpaqueRawPointer",
"OpaqueRawPointer",
"RawPointer"::["CString"]
} -> "CInt"]
}];execQuery = FunctionCompile[execDecs,
Function[{Typed[db, "Managed"::["SQLiteDB"]], Typed[query, "String"]},
Module[{cstrQuery},
cstrQuery = Cast[query, "Managed"::["CString"]];
LibraryFunction["sqlite3_exec"][
db,
cstrQuery,
Cast[0, "OpaqueRawPointer", "BitCast"],
Cast[0, "OpaqueRawPointer", "BitCast"],
Cast[0, "RawPointer"::["CString"], "BitCast"]
]
]
]
]dbFile = CreateFile[];db = openDB[dbFile]createTableQuery = "CREATE TABLE people (
id INTEGER PRIMARY KEY,
name TEXT
age INTEGER
);";execQuery[db, createTableQuery]これはエラーコード0を返す.表が作成できたということを意味する.
execQuery[db, createTableQuery]このexecquery関数はデータベース上で任意のクエリが実行できるが,クエリの結果を読み込む,または便利な形式で返すためのメカニズムはない.次のセクションではこれについて説明する.
読込み機能を加える
クエリ結果の走査
クエリ結果から読み込むためには,前とは少し異なるインターフェースを使う.sqlite3_execを使う代りに,sqlite3_prepareを使ってクエリ文を生成し,sqlite3_stepを使って結果に踏み込む.クエリ文を終了するためにsqlite3_finalizeも必要である.これらの型のシグネチャは次の通りである:
int sqlite3_prepare(
sqlite3 *db, /* Database handle */
const char *zSql, /* SQL statement, UTF-8 encoded */
int nByte, /* Maximum length of zSql in bytes. */
sqlite3_stmt **ppStmt, /* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zSql */
);
int sqlite3_step(sqlite3_stmt*);
int sqlite3_finalize(sqlite3_stmt *pStmt);
readingDecs = Join[
execDecs, {
TypeDeclaration["Alias", "SQLiteStatement", "OpaqueRawPointer"],
LibraryFunctionDeclaration["sqlite3_prepare", "libsqlite3", {
"SQLiteDB",
"CString",
"CInt",
"RawPointer"::["SQLiteStatement"],
"RawPointer"::["CString"]
} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_step", "libsqlite3", {"SQLiteStatement"} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_finalize", "libsqlite3", {"SQLiteStatement"} -> "CInt"]
}];readingDecs2 = Join[
readingDecs, {
FunctionDeclaration[createStatement,
Typed[{"Managed"::["SQLiteDB"], "String"} -> "Managed"::["SQLiteStatement"]]@
Function[{db, query},
Module[{cstrQuery, statementPointer},
cstrQuery = Cast[query, "Managed"::["CString"]];
statementPointer = Typed[ToRawPointer[], "RawPointer"::["SQLiteStatement"]];
LibraryFunction["sqlite3_prepare"][
db,
cstrQuery,
Cast[StringLength[query], "CInt", "ReinterpretCast"],
statementPointer,
Cast[0, "RawPointer"::["CString"], "BitCast"]
];
CreateTypeInstance["Managed", FromRawPointer[statementPointer], LibraryFunction["sqlite3_finalize"][#];&]
]
]
]
}];countResults = FunctionCompile[readingDecs2,
Function[{Typed[db, "Managed"::["SQLiteDB"]], Typed[query, "String"]},
Module[{statement, n},
statement = createStatement[db, query];
n = 0;
While[LibraryFunction["sqlite3_step"][statement] == 100, n++];
n
]
]
]With[{db = openDB[FindFile["ExampleData/ecommerce-database.sqlite"]]},
countResults[db, "SELECT * FROM customers;"]]行を式に変換する
上の関数により,クエリを実行し結果を走査することができる.しかし各行からデータを読み込みそれをWolfram言語式に変換するためにはライブラリ関数がもっと必要になる.特にこれらのSQLite関数は列数とその名前,型を取り出すのに使われる.
int sqlite3_column_count(sqlite3_stmt *pStmt);
const char *sqlite3_column_name(sqlite3_stmt*, int N);
int sqlite3_column_type(sqlite3_stmt*, int iCol);
readingDecs3 = Join[
readingDecs2, {
LibraryFunctionDeclaration["sqlite3_column_count", "libsqlite3", {"SQLiteStatement"} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_column_name", "libsqlite3", {"SQLiteStatement", "CInt"} -> "CString"],
LibraryFunctionDeclaration["sqlite3_column_type", "libsqlite3", {"SQLiteStatement", "CInt"} -> "CInt"]
}];これらのSQLite関数は各行のさまざまな型のデータを抽出するために使われる:
double sqlite3_column_double(sqlite3_stmt*, int iCol);
sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
readingDecs4 = Join[
readingDecs3, {
LibraryFunctionDeclaration["sqlite3_column_double", "libsqlite3", {"SQLiteStatement", "CInt"} -> "CDouble"],
LibraryFunctionDeclaration["sqlite3_column_int64", "libsqlite3", {"SQLiteStatement", "CInt"} -> "Integer64"],
LibraryFunctionDeclaration["sqlite3_column_text", "libsqlite3", {"SQLiteStatement", "CInt"} -> "CString"]
}];readingDecs5 = Join[
readingDecs4, {
FunctionDeclaration[columnExpression,
Typed[{"Managed"::["SQLiteStatement"], "MachineInteger"} -> "Expression"]@
Function[{statement, colIndex0},
With[{colIndex = Cast[colIndex0, "CInt", "ReinterpretCast"]},
Switch[LibraryFunction["sqlite3_column_type"][statement, colIndex],
Typed[1, "CInt"],
Cast[LibraryFunction["sqlite3_column_int64"][statement, colIndex], "Expression"],
Typed[2, "CInt"],
Cast[LibraryFunction["sqlite3_column_double"][statement, colIndex], "Expression"],
Typed[3, "CInt"],
Cast[CreateTypeInstance["String", LibraryFunction["sqlite3_column_text"][statement, colIndex]], "Expression"],
Typed[5, "CInt"],
InertExpression[Missing["NullValue"]],
_,
InertExpression[Missing["UnsupportedType"]]
]
]
]
]
}];readingDecs6 = Join[
readingDecs5, {
FunctionDeclaration[rowAssociation,
Typed[{"Managed"::["SQLiteStatement"]} -> "Expression"]@
Function[statement,
Module[{columnCount, columnRules},
columnCount = Cast[LibraryFunction["sqlite3_column_count"][statement], "MachineInteger", "ReinterpretCast"];
columnRules = (col |-> Module[{colName, colRule},
colName = LibraryFunction["sqlite3_column_name"][statement, Cast[col, "CInt", "ReinterpretCast"]];
colRule = CreateTypeInstance["String", colName] -> columnExpression[statement, col];
Cast[colRule, "Expression"]
]) /@ Cast[Range[0, columnCount - 1], "ListVector"::[_]];
InertExpression[Association]@@columnRules
]
]
]
}];クエリ関数のコンパイル
openDBを使ってデータベースを開き自動的に閉じることができるようになったので,createStatementでクエリをコンパイルすることができる.クエリ結果の各行はrowAssociationで式に変換することができる.後はそれをまとめるだけである.
queryDB = FunctionCompile[readingDecs6,
Function[{Typed[db, "Managed"::["SQLiteDB"]], Typed[query, "String"]},
Module[{statement, out},
statement = createStatement[db, query];
out = CreateDataStructure["DynamicArray"];
While[LibraryFunction["sqlite3_step"][statement] === Typed[100, "CInt"],
AppendTo[out, rowAssociation[statement]]
];
Normal[out]
]
]
]db = openDB[FindFile["ExampleData/ecommerce-database.sqlite"]]queryDB[db, "SELECT * FROM customers WHERE creditLimit > 200000;"]Dataset[queryDB[db, "SELECT * FROM customers;"]]完全なコード
{openDB, queryDB} = FunctionCompile[{
TypeDeclaration["Alias", "SQLiteDB", "OpaqueRawPointer"],
TypeDeclaration["Alias", "SQLiteStatement", "OpaqueRawPointer"],
LibraryFunctionDeclaration["sqlite3_open", "libsqlite3", {"CString", "RawPointer"::["SQLiteDB"]} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_close", "libsqlite3", {"SQLiteDB"} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_prepare", "libsqlite3", {
"SQLiteDB",
"CString",
"CInt",
"RawPointer"::["SQLiteStatement"],
"RawPointer"::["CString"]
} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_step", "libsqlite3", {"SQLiteStatement"} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_finalize", "libsqlite3", {"SQLiteStatement"} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_column_count", "libsqlite3", {"SQLiteStatement"} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_column_name", "libsqlite3", {"SQLiteStatement", "CInt"} -> "CString"],
LibraryFunctionDeclaration["sqlite3_column_type", "libsqlite3", {"SQLiteStatement", "CInt"} -> "CInt"],
LibraryFunctionDeclaration["sqlite3_column_double", "libsqlite3", {"SQLiteStatement", "CInt"} -> "CDouble"],
LibraryFunctionDeclaration["sqlite3_column_int64", "libsqlite3", {"SQLiteStatement", "CInt"} -> "Integer64"],
LibraryFunctionDeclaration["sqlite3_column_text", "libsqlite3", {"SQLiteStatement", "CInt"} -> "CString"],
FunctionDeclaration[createStatement,
Typed[{"Managed"::["SQLiteDB"], "String"} -> "Managed"::["SQLiteStatement"]]@
Function[{db, query},
Module[{cstrQuery, statementPointer},
cstrQuery = Cast[query, "Managed"::["CString"]];
statementPointer = Typed[ToRawPointer[], "RawPointer"::["SQLiteStatement"]];
LibraryFunction["sqlite3_prepare"][
db,
cstrQuery,
Cast[StringLength[query], "CInt", "ReinterpretCast"],
statementPointer,
Cast[0, "RawPointer"::["CString"], "BitCast"]
];
CreateTypeInstance["Managed", FromRawPointer[statementPointer], LibraryFunction["sqlite3_finalize"][#];&]
]
]
],
FunctionDeclaration[columnExpression,
Typed[{"Managed"::["SQLiteStatement"], "MachineInteger"} -> "Expression"]@
Function[{statement, colIndex0},
With[{colIndex = Cast[colIndex0, "CInt", "ReinterpretCast"]},
Switch[LibraryFunction["sqlite3_column_type"][statement, colIndex],
Typed[1, "CInt"],
Cast[LibraryFunction["sqlite3_column_int64"][statement, colIndex], "Expression"],
Typed[2, "CInt"],
Cast[LibraryFunction["sqlite3_column_double"][statement, colIndex], "Expression"],
Typed[3, "CInt"],
Cast[CreateTypeInstance["String", LibraryFunction["sqlite3_column_text"][statement, colIndex]], "Expression"],
Typed[5, "CInt"],
InertExpression[Missing["NullValue"]],
_,
InertExpression[Missing["UnsupportedType"]]
]
]
]
],
FunctionDeclaration[rowAssociation,
Typed[{"Managed"::["SQLiteStatement"]} -> "Expression"]@
Function[statement,
Module[{columnCount, columnRules},
columnCount = Cast[LibraryFunction["sqlite3_column_count"][statement], "MachineInteger", "ReinterpretCast"];
columnRules = (col |-> Module[{colName, colRule},
colName = LibraryFunction["sqlite3_column_name"][statement, Cast[col, "CInt", "ReinterpretCast"]];
colRule = CreateTypeInstance["String", colName] -> columnExpression[statement, col];
Cast[colRule, "Expression"]
]) /@ Cast[Range[0, columnCount - 1], "ListVector"::[_]];
InertExpression[Association]@@columnRules
]
]
]
}, {
Function[Typed[path, "String"],
Module[{cstrPath, dbPointer},
cstrPath = Cast[path, "Managed"::["CString"]];
dbPointer = Typed[ToRawPointer[], "RawPointer"::["SQLiteDB"]];
LibraryFunction["sqlite3_open"][cstrPath, dbPointer];
CreateTypeInstance["Managed", FromRawPointer[dbPointer], LibraryFunction["sqlite3_close"][#];&]
]
],
Function[{Typed[db, "Managed"::["SQLiteDB"]], Typed[query, "String"]},
Module[{statement, out},
statement = createStatement[db, query];
out = CreateDataStructure["DynamicArray"];
While[LibraryFunction["sqlite3_step"][statement] === Typed[100, "CInt"],
AppendTo[out, rowAssociation[statement]]
];
Normal[out]
]
]
}]