Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Addressed comments from apple/swift#25147. #140

Merged
merged 4 commits into from
May 30, 2019
Merged
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
21 changes: 11 additions & 10 deletions Sources/TensorFlow/Core/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,12 @@ public final class _ExecutionContext {
// Initialize the TF runtime exactly once. Only affects local execution
// (when _RuntimeConfig.tensorFlowServer is set to "").
if !_RuntimeConfig.tensorFlowRuntimeInitialized {
// Install a signal handler to ensure we exit when interrupted.
signal(SIGINT) { _ in
print("Caught interrupt signal, exiting...")
exit(1)
}

var args = ["dummyProgramName"]
if _RuntimeConfig.printsDebugLog {
args.append("--alsologtostderr")
Expand All @@ -588,24 +594,19 @@ public final class _ExecutionContext {

// Calculate the addresses of all the strings within our single buffer, and then call
// TF_InitMain.
flattenedStringBytes.withUnsafeMutableBufferPointer { flattenedStringBytesBuffer in
flattenedStringBytes.withUnsafeMutableBufferPointer { buffer in
var stringAddrs: [UnsafeMutablePointer<Int8>?] = []
var currentStringAddr = flattenedStringBytesBuffer.baseAddress
var currentStringAddr = buffer.baseAddress
.map(UnsafeMutablePointer.init)
for length in lengths {
stringAddrs.append(currentStringAddr)
currentStringAddr = currentStringAddr?.advanced(by: length)
}

stringAddrs.withUnsafeMutableBufferPointer { stringAddrsBuffer in
var cArgs = [stringAddrsBuffer.baseAddress.map(UnsafeMutablePointer.init)]
var cArgsCount = [Int32(args.count)]

cArgs.withUnsafeMutableBufferPointer { cArgsBuffer in
cArgsCount.withUnsafeMutableBufferPointer { cArgsCountBuffer in
TF_InitMain(nil, cArgsCountBuffer.baseAddress, cArgsBuffer.baseAddress)
}
}
var cArgsCount = Int32(args.count)
var cArgs = stringAddrsBuffer.baseAddress.map(UnsafeMutablePointer.init)
TF_InitMain(nil, &cArgsCount, &cArgs)
}
}
_RuntimeConfig.tensorFlowRuntimeInitialized = true
Expand Down