1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 // Header guard and namespace names follow OpenMP runtime conventions.
18 
19 #ifndef KMP_RML_OMP_H
20 #define KMP_RML_OMP_H
21 
22 #include "rml_base.h"
23 
24 namespace __kmp {
25 namespace rml {
26 
27 class omp_client;
28 
29 //------------------------------------------------------------------------
30 // Classes instantiated by the server
31 //------------------------------------------------------------------------
32 
33 //! Represents a set of omp worker threads provided by the server.
34 class omp_server: public ::rml::server {
35 public:
36     //! A number of coins (i.e., threads)
37     typedef unsigned size_type;
38 
39     //! Return the number of coins in the bank. (negative if machine is oversubscribed).
40     virtual int current_balance() const = 0;
41 
42     //! Request n coins.  Returns number of coins granted. Oversubscription amount if negative.
43     /** Always granted if is_strict is true.
44         - Positive or zero result indicates that the number of coins was taken from the bank.
45         - Negative result indicates that no coins were taken, and that the bank has deficit
46           by that amount and the caller (if being a good citizen) should return that many coins.
47      */
48     virtual int try_increase_load( size_type /*n*/, bool /*strict*/ ) = 0;
49 
50     //! Return n coins into the bank.
51     virtual void decrease_load( size_type /*n*/ ) = 0;
52 
53     //! Convert n coins into n threads.
54     /** When a thread returns, it is converted back into a coin and the coin is returned to the bank. */
55     virtual void get_threads( size_type /*m*/, void* /*cookie*/, job* /*array*/[] ) = 0;
56 
57     /** Putting a thread to sleep - convert a thread into a coin
58         Waking up a thread        - convert a coin into a thread
59 
60        Note: conversion between a coin and a thread does not affect the accounting.
61      */
62 #if _WIN32||_WIN64
63     //! Inform server of a TBB master thread.
64     virtual void register_master( execution_resource_t& /*v*/ ) = 0;
65 
66     //! Inform server that the TBB master thread is done with its work.
67     virtual void unregister_master( execution_resource_t /*v*/ ) = 0;
68 
69     //! deactivate
70     /** give control to ConcRT RM */
71     virtual void deactivate( job* ) = 0;
72 
73     //! reactivate
74     virtual void reactivate( job* ) = 0;
75 #endif /* _WIN32||_WIN64 */
76 };
77 
78 
79 //------------------------------------------------------------------------
80 // Classes (or base classes thereof) instantiated by the client
81 //------------------------------------------------------------------------
82 
83 class omp_client: public ::rml::client {
84 public:
85     //! Called by server thread when it delivers a thread to client
86     /** The index argument is a 0-origin index of the job for this thread within the array
87         returned by method get_threads.  Server decreases the load by 1 (i.e., returning the coin
88         back to the bank) after this method returns. */
89     virtual void process( job&, void* /*cookie*/, size_type /*index*/ ) RML_PURE(void)
90 };
91 
92 /** Client must ensure that instance is zero-inited, typically by being a file-scope object. */
93 class omp_factory: public ::rml::factory {
94 
95     //! Pointer to routine that creates an RML server.
96     status_type (*my_make_server_routine)( omp_factory&, omp_server*&, omp_client& );
97 
98     //! Pointer to routine that calls callback function with server version info.
99     void (*my_call_with_server_info_routine)( ::rml::server_info_callback_t cb, void* arg );
100 
101 public:
102     typedef ::rml::versioned_object::version_type version_type;
103     typedef omp_client client_type;
104     typedef omp_server server_type;
105 
106     //! Open factory.
107     /** Dynamically links against RML library.
108         Returns st_success, st_incompatible, or st_not_found. */
109     status_type open();
110 
111     //! Factory method to be called by client to create a server object.
112     /** Factory must be open.
113         Returns st_success or st_incompatible . */
114     status_type make_server( server_type*&, client_type& );
115 
116     //! Close factory.
117     void close();
118 
119     //! Call the callback with the server build info.
120     void call_with_server_info( ::rml::server_info_callback_t cb, void* arg ) const;
121 };
122 
123 } // namespace rml
124 } // namespace __kmp
125 
126 #endif /* KMP_RML_OMP_H */
127