File: getDoSeq.R

package info (click to toggle)
r-cran-foreach 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 648 kB
  • sloc: makefile: 2
file content (96 lines) | stat: -rw-r--r-- 3,251 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#
# Copyright (c) Microsoft. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#' @name getDoSeqWorkers
#' @title Functions Providing Information on the doSeq Backend
#' @description
#' The `getDoSeqWorkers` function returns the number of
#' execution workers there are in the currently registered doSeq backend.
#' A `1` is returned by default.
#'
#' The `getDoSeqRegistered` function returns TRUE if a doSeq backend
#' has been registered, otherwise FALSE.
#'
#' The `getDoSeqName` function returns the name of the currently
#' registered doSeq backend.  A `NULL` is returned if no backend is
#' registered.
#'
#' The `getDoSeqVersion` function returns the version of the currently
#' registered doSeq backend.  A `NULL` is returned if no backend is
#' registered.
#'
#' @examples
#' cat(sprintf('%s backend is registered\n',
#'             if(getDoSeqRegistered()) 'A' else 'No'))
#' cat(sprintf('Running with %d worker(s)\n', getDoSeqWorkers()))
#' (name <- getDoSeqName())
#' (ver <- getDoSeqVersion())
#' if (getDoSeqRegistered())
#'   cat(sprintf('Currently using %s [%s]\n', name, ver))
#'
#' @keywords utilities
# this returns a logical value indicating if a sequential backend
# has been registered or not
#' @export
#' @rdname getDoSeqWorkers
getDoSeqRegistered <- function() {
  exists('seqFun', where=.foreachGlobals, inherits=FALSE)
}

# this returns the number of workers used by the currently registered
# sequential backend
#' @export
#' @rdname getDoSeqWorkers
getDoSeqWorkers <- function() {
  wc <- if (exists('seqInfo', where=.foreachGlobals, inherits=FALSE))
    .foreachGlobals$seqInfo(.foreachGlobals$seqData, 'workers')
  else
    NULL

  # interpret a NULL as a single worker, but the backend
  # can return NA without interference
  if (is.null(wc)) 1L else wc
}

# this returns the name of the currently registered sequential backend
#' @export
#' @rdname getDoSeqWorkers
getDoSeqName <- function() {
  if (exists('seqInfo', where=.foreachGlobals, inherits=FALSE))
    .foreachGlobals$seqInfo(.foreachGlobals$seqData, 'name')
  else
    NULL
}

# this returns the version of the currently registered sequential backend
#' @export
#' @rdname getDoSeqWorkers
getDoSeqVersion <- function() {
  if (exists('seqInfo', where=.foreachGlobals, inherits=FALSE))
    .foreachGlobals$seqInfo(.foreachGlobals$seqData, 'version')
  else
    NULL
}

# used internally to get the currently registered parallel backend
getDoSeq <- function() {
  if (exists('seqFun', where=.foreachGlobals, inherits=FALSE)) {
    list(fun=.foreachGlobals$seqFun, data=.foreachGlobals$seqdata)
  } else {
    list(fun=doSEQ, data=NULL)
  }
}