com.powsybl.computation.mpi.CorePool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powsybl-computation-mpi Show documentation
Show all versions of powsybl-computation-mpi Show documentation
A computation implementation to run computations with MPI
The newest version!
/**
* Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.computation.mpi;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Set;
/**
*
* @author Geoffroy Jamgotchian {@literal }
*/
class CorePool {
private final Deque cores = new ArrayDeque<>();
private final Multimap coresPerRank = HashMultimap.create();
List borrowCores(int n) {
return borrowCores(n, null);
}
synchronized List borrowCores(int n, Set preferedRanks) {
checkCores(n);
List borrowedCores = new ArrayList<>(n);
if (preferedRanks != null && !preferedRanks.isEmpty()) {
END: for (int preferedRank : preferedRanks) {
for (Core preferedCore : coresPerRank.get(preferedRank)) {
if (borrowedCores.size() >= n) {
break END;
}
borrowedCores.add(preferedCore);
}
}
for (Core borrowedCore : borrowedCores) {
cores.remove(borrowedCore);
coresPerRank.remove(borrowedCore.rank, borrowedCore);
}
}
while (borrowedCores.size() < n) {
Core borrowedCore = cores.poll();
borrowedCores.add(borrowedCore);
coresPerRank.remove(borrowedCore.rank, borrowedCore);
}
return borrowedCores;
}
private void checkCores(int n) {
if (n > cores.size()) {
throw new IllegalArgumentException("Not enough cores");
}
}
synchronized void returnCore(Core borrowedCore) {
cores.push(borrowedCore);
coresPerRank.put(borrowedCore.rank.num, borrowedCore);
}
void returnCores(List borrowedCores) {
for (Core borrowedCore : borrowedCores) {
returnCore(borrowedCore);
}
}
synchronized int availableCores() {
return cores.size();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy