NAG FL Interface
m01ndf (realvec_vec_search)
1
Purpose
m01ndf searches a strictly ordered vector of real numbers and returns the indices of the largest numbers in the vector which are smaller than or equal to the sought-after items.
2
Specification
Fortran Interface
Subroutine m01ndf ( |
valid, mode, rv, n, m1, m2, item, m, idx, h, k, lk, ifail) |
Integer, Intent (In) |
:: |
mode, n, m1, m2, m |
Integer, Intent (Inout) |
:: |
k(lk), lk, ifail |
Integer, Intent (Out) |
:: |
idx(m) |
Real (Kind=nag_wp), Intent (In) |
:: |
rv(n), item(m) |
Real (Kind=nag_wp), Intent (Inout) |
:: |
h |
Logical, Intent (In) |
:: |
valid |
|
C Header Interface
#include <nag.h>
void |
m01ndf_ (const logical *valid, const Integer *mode, const double rv[], const Integer *n, const Integer *m1, const Integer *m2, const double item[], const Integer *m, Integer idx[], double *h, Integer k[], Integer *lk, Integer *ifail) |
|
C++ Header Interface
#include <nag.h> extern "C" {
void |
m01ndf_ (const logical &valid, const Integer &mode, const double rv[], const Integer &n, const Integer &m1, const Integer &m2, const double item[], const Integer &m, Integer idx[], double &h, Integer k[], Integer &lk, Integer &ifail) |
}
|
The routine may be called by the names m01ndf or nagf_sort_realvec_vec_search.
3
Description
m01ndf searches an array, , of strictly-increasing real numbers, , , for the elements of an unordered array of real numbers, . If a value equal to a sought-after item is not found in , the index of the immediate lower value is returned. If is less than , is returned.
The routine implements the direct search algorithm presented as Algorithm 8 in
Cannizzo (2018). It precomputes a scalar,
, and a reference vector of indices,
, that it uses to speed up searches of
. The length of
is a function of the number of entries in
and their values, and the routine provides a mechanism to compute what this length should be.
If the amount of memory required for
is infeasible,
m01ndf
implements offset-based binary search (Algorithm 5 in
Cannizzo (2018)) as an alternative that does not require
to be used.
See
Section 9 for more information on the relative time complexities of the two search algorithms.
4
References
Cannizzo F (2018) A fast and vectorizable alternative to binary search in O(1) with wide applicability to arrays of floating point numbers Journal of Parallel and Distributed Computing 113 37–54
5
Arguments
-
1:
– Logical
Input
-
On entry: if
valid is set to .TRUE. argument checking will be performed. If
valid is set to .FALSE.
m01ndf will be called without argument checking (which includes checking that array
rv is sorted in strictly ascending order). See
Section 9 for further details.
-
2:
– Integer
Input
-
On entry: a code for selecting the operation to be performed by the routine. The first call to
m01ndf must be made with
, and this must be followed by a second call with
. Thereafter repeated searches of
rv can be made through repeated calls with
.
- Compute h and lk. Following a call with , k must be allocated to hold lk elements and then the routine must be called again with .
- Set up k (the reference vector) using the values of h and lk returned from a prior call to m01ndf with .
- Direct search using h and k set up in prior calls to m01ndf with or .
- Search using offset-based binary search, which does not require h or k to be used.
Constraint:
, , or .
-
3:
– Real (Kind=nag_wp) array
Input
-
On entry: , the real values to be searched.
Constraint:
elements of
rv must be sorted in strictly ascending order.
-
4:
– Integer
Input
-
On entry: , the number of real values to be searched.
Constraint:
.
-
5:
– Integer
Input
-
On entry: the index of the first element of
rv to be searched.
Constraint:
.
-
6:
– Integer
Input
-
On entry: the index of the last element of
rv to be searched.
Constraint:
.
-
7:
– Real (Kind=nag_wp) array
Input
-
On entry: , the sought-after items.
-
8:
– Integer
Input
-
On entry: , the number of items sought.
Constraint:
.
-
9:
– Integer array
Output
-
On exit: if
,
contains the index of the largest entry in
rv which is smaller than or equal to
.
-
10:
– Real (Kind=nag_wp)
Communication
-
On entry:
, the reference scalar required by the direct search algorithm. If
,
m01ndf calculates the value of
h required by the direct search routine for the given values in
rv. Otherwise, the value of
h as declared in the (sub)routine from which
m01ndf is called. If
,
h is not referenced.
On exit: the value of
h required by the direct search routine for the given values in
rv.
Constraint:
if
or
,
h must be unchanged from the previous call to
m01ndf.
-
11:
– Integer array
Communication Array
-
On entry: if
,
k, the reference vector from the previous call to
m01ndf. If
or
,
k is not referenced.
On exit: if or , the reference vector.
Constraint:
if
,
k must be unchanged from the previous call to
m01ndf.
-
12:
– Integer
Input/Output
-
On entry: if
,
m01ndf calculates the dimension of
k required by the direct search routine for the given values in
rv. Otherwise, the dimension of the array
k as declared in the (sub)routine from which
m01ndf is called. If
,
lk is not referenced.
On exit: if
, the dimension of the array
k required by the direct search routine for the given values in
rv. Otherwise, the dimension of the array
k as declared in the (sub)routine from which
m01ndf is called.
Constraint:
if
or
,
lk must be unchanged from the previous call to
m01ndf.
-
13:
– Integer
Input/Output
-
On entry:
ifail must be set to
,
or
to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of means that an error message is printed while a value of means that it is not.
If halting is not appropriate, the value
or
is recommended. If message printing is undesirable, then the value
is recommended. Otherwise, the value
is recommended.
When the value or is used it is essential to test the value of ifail on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Errors or warnings detected by the routine:
-
On entry, .
Constraint: , , or .
-
On entry,
rv must be sorted in strictly ascending order:
.
-
On entry, .
Constraint: .
-
On entry, , , .
Constraint: .
-
On entry, .
Constraint: .
-
On entry, .
Constraint: .
-
On entry, the values in
rv are such that the required value of
lk would
overflow the current platform's largest positive integer value. This error
should only appear when
m01ndf is called with
(i.e., when the routine is asked to calculate the required value of
lk for the given
rv).
-
Either
m01ndf was not called with
or
,
or the values of
h,
k or
lk may have become corrupted.
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 7 in the Introduction to the NAG Library FL Interface for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 8 in the Introduction to the NAG Library FL Interface for further information.
Dynamic memory allocation failed.
See
Section 9 in the Introduction to the NAG Library FL Interface for further information.
7
Accuracy
Not applicable.
8
Parallelism and Performance
m01ndf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the
X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the
Users' Note for your implementation for any additional implementation-specific information.
The argument
valid should be used with caution. Set it to .FALSE. only if you are confident that the other arguments are correct, in particular that array
rv is in fact arranged in strictly ascending order. If you wish to search the same array
rv many times, you are recommended to set
valid to .TRUE. on the first call of
m01ndf and to .FALSE. on subsequent calls, in order to minimize the amount of time spent checking
rv, which may be significant if
rv is large.
The time taken by
m01ndf to construct the reference vector (i.e., when the routine is called with
) is
. Note that the values stored in
k depend on all entries of
rv, and not just those in the interval
. Thereafter, searching for
m items using direct search (i.e., when the routine is called with
) is
. In contrast offset-based binary search (i.e., when the routine is called with
), does not require construction of the reference vector and has time complexity
. Although this is the same as classical binary search, offset-based binary search may be faster in practice because the implementation does not feature conditional branches. Direct search is preferable when the overhead of constructing the reference vector can be offset by conducting multiple searches on an unchanged
rv.
10
Example
This example reads a list of real numbers and a list of sought-after items and performs the search for these items. The example demonstrates how to use m01ndf for both direct search (i.e., ) and offset-based binary search ().
10.1
Program Text
10.2
Program Data
10.3
Program Results