1    	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2    	/*                                                                           */
3    	/*                  This file is part of the program and library             */
4    	/*         SCIP --- Solving Constraint Integer Programs                      */
5    	/*                                                                           */
6    	/*  Copyright (c) 2002-2023 Zuse Institute Berlin (ZIB)                      */
7    	/*                                                                           */
8    	/*  Licensed under the Apache License, Version 2.0 (the "License");          */
9    	/*  you may not use this file except in compliance with the License.         */
10   	/*  You may obtain a copy of the License at                                  */
11   	/*                                                                           */
12   	/*      http://www.apache.org/licenses/LICENSE-2.0                           */
13   	/*                                                                           */
14   	/*  Unless required by applicable law or agreed to in writing, software      */
15   	/*  distributed under the License is distributed on an "AS IS" BASIS,        */
16   	/*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17   	/*  See the License for the specific language governing permissions and      */
18   	/*  limitations under the License.                                           */
19   	/*                                                                           */
20   	/*  You should have received a copy of the Apache-2.0 license                */
21   	/*  along with SCIP; see the file LICENSE. If not visit scipopt.org.         */
22   	/*                                                                           */
23   	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24   	
25   	/**@file   scip_concurrent.c
26   	 * @ingroup OTHER_CFILES
27   	 * @brief  public methods for concurrent solving mode
28   	 * @author Tobias Achterberg
29   	 * @author Timo Berthold
30   	 * @author Gerald Gamrath
31   	 * @author Leona Gottwald
32   	 * @author Stefan Heinz
33   	 * @author Gregor Hendel
34   	 * @author Thorsten Koch
35   	 * @author Alexander Martin
36   	 * @author Marc Pfetsch
37   	 * @author Michael Winkler
38   	 * @author Kati Wolter
39   	 *
40   	 * @todo check all SCIP_STAGE_* switches, and include the new stages TRANSFORMED and INITSOLVE
41   	 */
42   	
43   	/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
44   	
45   	#include "scip/concsolver.h"
46   	#include "scip/debug.h"
47   	#include "scip/pub_message.h"
48   	#include "scip/scip_concurrent.h"
49   	#include "scip/set.h"
50   	#include "scip/struct_mem.h"
51   	#include "scip/struct_scip.h"
52   	#include "scip/struct_set.h"
53   	#include "scip/syncstore.h"
54   	
55   	/** creates a concurrent solver type and includes it in SCIP.
56   	 *
57   	 *  @return \ref SCIP_OKAY is returned if everything worked. otherwise a suitable error code is passed. see \ref
58   	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
59   	 *
60   	 *  @pre This method can be called if @p scip is in one of the following stages:
61   	 *       - \ref SCIP_STAGE_INIT
62   	 *       - \ref SCIP_STAGE_PROBLEM
63   	 */
64   	SCIP_RETCODE SCIPincludeConcsolverType(
65   	   SCIP*                               scip,                       /**< SCIP data structure */
66   	   const char*                         name,                       /**< name of concurrent_solver */
67   	   SCIP_Real                           prefpriodefault,            /**< the default preferred priority of this concurrent solver type */
68   	   SCIP_DECL_CONCSOLVERCREATEINST      ((*concsolvercreateinst)),  /**< data copy method of concurrent solver */
69   	   SCIP_DECL_CONCSOLVERDESTROYINST     ((*concsolverdestroyinst)), /**< data copy method of concurrent solver */
70   	   SCIP_DECL_CONCSOLVERINITSEEDS       ((*concsolverinitseeds)),   /**< initialize random seeds of concurrent solver */
71   	   SCIP_DECL_CONCSOLVEREXEC            ((*concsolverexec)),        /**< execution method of concurrent solver */
72   	   SCIP_DECL_CONCSOLVERCOPYSOLVINGDATA ((*concsolvercopysolvdata)),/**< method to copy solving data */
73   	   SCIP_DECL_CONCSOLVERSTOP            ((*concsolverstop)),        /**< terminate solving in concurrent solver */
74   	   SCIP_DECL_CONCSOLVERSYNCWRITE       ((*concsolversyncwrite)),   /**< synchronization method of concurrent solver */
75   	   SCIP_DECL_CONCSOLVERSYNCREAD        ((*concsolversyncread)),    /**< synchronization method of concurrent solver */
76   	   SCIP_DECL_CONCSOLVERTYPEFREEDATA    ((*concsolvertypefreedata)),/**< method to free data of concurrent solver type */
77   	   SCIP_CONCSOLVERTYPEDATA*            data                        /**< the concurent solver type's data */
78   	   )
79   	{
80   	   SCIP_CONCSOLVERTYPE* concsolvertype;
81   	
82   	   SCIP_CALL( SCIPcheckStage(scip, "SCIPincludeConcsolverType", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
83   	
84   	   /* check whether concurrent solver type is already present */
85   	   if( SCIPfindConcsolverType(scip, name) != NULL )
86   	   {
87   	      SCIPerrorMessage("concurrent solver type <%s> already included.\n", name);
88   	      return SCIP_INVALIDDATA;
89   	   }
90   	
91   	   SCIP_CALL( SCIPconcsolverTypeCreate(&concsolvertype, scip->set, scip->messagehdlr, scip->mem->setmem,
92   	         name, prefpriodefault, concsolvercreateinst, concsolverdestroyinst,
93   	         concsolverinitseeds, concsolverexec, concsolvercopysolvdata,
94   	         concsolverstop, concsolversyncwrite, concsolversyncread,
95   	         concsolvertypefreedata, data) );
96   	
97   	   SCIP_CALL( SCIPsetIncludeConcsolverType(scip->set, concsolvertype) );
98   	
99   	   return SCIP_OKAY;
100  	}
101  	
102  	/** returns the concurrent solver type with the given name, or NULL if not existing */
103  	SCIP_CONCSOLVERTYPE* SCIPfindConcsolverType(
104  	   SCIP*                 scip,               /**< SCIP data structure */
105  	   const char*           name                /**< name of concurrent_solver */
106  	   )
107  	{
108  	   assert(scip != NULL);
109  	   assert(scip->set != NULL);
110  	   assert(name != NULL);
111  	
112  	   return SCIPsetFindConcsolverType(scip->set, name);
113  	}
114  	
115  	/** returns the array of included concurrent solver types */
116  	SCIP_CONCSOLVERTYPE** SCIPgetConcsolverTypes(
117  	   SCIP*                 scip                /**< SCIP data structure */
118  	   )
119  	{
120  	   assert(scip != NULL);
121  	   assert(scip->set != NULL);
122  	
123  	   return scip->set->concsolvertypes;
124  	}
125  	
126  	/** returns the number of included concurrent solver types */
127  	int SCIPgetNConcsolverTypes(
128  	   SCIP*                 scip                /**< SCIP data structure */
129  	   )
130  	{
131  	   assert(scip != NULL);
132  	   assert(scip->set != NULL);
133  	
134  	   return scip->set->nconcsolvertypes;
135  	}
136  	
137  	/** Constructs the parallel interface to execute processes concurrently.
138  	 *
139  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
140  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
141  	 *
142  	 *  @pre This method can be called if @p scip is in one of the following stages:
143  	 *       - \ref SCIP_STAGE_PROBLEM
144  	 *       - \ref SCIP_STAGE_TRANSFORMING
145  	 *       - \ref SCIP_STAGE_TRANSFORMED
146  	 *       - \ref SCIP_STAGE_INITPRESOLVE
147  	 *       - \ref SCIP_STAGE_PRESOLVING
148  	 *       - \ref SCIP_STAGE_EXITPRESOLVE
149  	 *       - \ref SCIP_STAGE_PRESOLVED
150  	 *       - \ref SCIP_STAGE_INITSOLVE
151  	 *       - \ref SCIP_STAGE_SOLVING
152  	 *       - \ref SCIP_STAGE_SOLVED
153  	 *       - \ref SCIP_STAGE_EXITSOLVE
154  	 *       - \ref SCIP_STAGE_FREETRANS
155  	 *
156  	 *  See \ref SCIP_Stage "SCIP_STAGE" for a complete list of all possible solving stages.
157  	 */
158  	SCIP_RETCODE SCIPconstructSyncstore(
159  	   SCIP*                 scip                /**< SCIP data structure */
160  	   )
161  	{
162  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPconstructSyncstore", FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE) );
163  	
164  	   SCIP_CALL( SCIPsyncstoreCreate(&scip->syncstore) );
165  	
166  	   return SCIP_OKAY;
167  	}
168  	
169  	/** releases the current parallel interface
170  	 *
171  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
172  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
173  	 *
174  	 *  @pre This method can be called if @p scip is in one of the following stages:
175  	 *       - \ref SCIP_STAGE_PROBLEM
176  	 *       - \ref SCIP_STAGE_TRANSFORMING
177  	 *       - \ref SCIP_STAGE_TRANSFORMED
178  	 *       - \ref SCIP_STAGE_INITPRESOLVE
179  	 *       - \ref SCIP_STAGE_PRESOLVING
180  	 *       - \ref SCIP_STAGE_EXITPRESOLVE
181  	 *       - \ref SCIP_STAGE_PRESOLVED
182  	 *       - \ref SCIP_STAGE_INITSOLVE
183  	 *       - \ref SCIP_STAGE_SOLVING
184  	 *       - \ref SCIP_STAGE_SOLVED
185  	 *       - \ref SCIP_STAGE_EXITSOLVE
186  	 *       - \ref SCIP_STAGE_FREETRANS
187  	 *       - \ref SCIP_STAGE_FREE
188  	 *
189  	 *  See \ref SCIP_Stage "SCIP_STAGE" for a complete list of all possible solving stages.
190  	 */
191  	SCIP_RETCODE SCIPfreeSyncstore(
192  	   SCIP*                 scip                /**< SCIP data structure */
193  	   )
194  	{
195  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPfreeSyncstore", FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) );
196  	
197  	   SCIP_CALL( SCIPsyncstoreRelease(&(scip->syncstore)) );
198  	
199  	   return SCIP_OKAY;
200  	}
201  	
202  	/** Gets the parallel interface to execute processes concurrently.
203  	 *
204  	 *  @return the \ref SCIP_SYNCSTORE parallel interface pointer to submit jobs for concurrent processing.
205  	 *
206  	 *  @pre This method can be called if @p scip is in one of the following stages:
207  	 *       - \ref SCIP_STAGE_INIT
208  	 *       - \ref SCIP_STAGE_PROBLEM
209  	 *       - \ref SCIP_STAGE_TRANSFORMING
210  	 *       - \ref SCIP_STAGE_TRANSFORMED
211  	 *       - \ref SCIP_STAGE_INITPRESOLVE
212  	 *       - \ref SCIP_STAGE_PRESOLVING
213  	 *       - \ref SCIP_STAGE_EXITPRESOLVE
214  	 *       - \ref SCIP_STAGE_PRESOLVED
215  	 *       - \ref SCIP_STAGE_INITSOLVE
216  	 *       - \ref SCIP_STAGE_SOLVING
217  	 *       - \ref SCIP_STAGE_SOLVED
218  	 *       - \ref SCIP_STAGE_EXITSOLVE
219  	 *       - \ref SCIP_STAGE_FREETRANS
220  	 *
221  	 *  See \ref SCIP_Stage "SCIP_STAGE" for a complete list of all possible solving stages.
222  	 */
223  	SCIP_SYNCSTORE* SCIPgetSyncstore(
224  	   SCIP*                 scip                /**< SCIP data structure */
225  	   )
226  	{
227  	   SCIP_CALL_ABORT( SCIPcheckStage(scip, "SCIPgetSyncstore", TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE) );
228  	
229  	   return scip->syncstore;
230  	}
231