
org.deeplearning4j.iterativereduce.runtime.Utils Maven / Gradle / Ivy
The newest version!
/*
*
* * Copyright 2015 Skymind,Inc.
* *
* * 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.
*
*/
package org.deeplearning4j.iterativereduce.runtime;
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapred.InputSplit;
import org.deeplearning4j.iterativereduce.runtime.yarn.avro.generated.StartupConfiguration;
import org.deeplearning4j.iterativereduce.runtime.yarn.avro.generated.WorkerId;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.yarn.api.ApplicationConstants;
import org.apache.hadoop.yarn.api.records.*;
import org.apache.hadoop.yarn.util.ConverterUtils;
import org.apache.hadoop.yarn.util.Records;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
/*
* TODO: fix to more sane method of copying collections (e.g. Collections.addAll
*
*/
public class Utils {
private static Log LOG = LogFactory.getLog(Utils.class);
public static String getWorkerId(WorkerId workerId) {
return new String(workerId.bytes()).trim();
}
/**
* Generate an input split from the specified file split
* @param split the split to generate
* @return the file split for the specified split
*/
public static InputSplit getSplit(org.deeplearning4j.iterativereduce.runtime.yarn.avro.generated.FileSplit split) {
Path path = new Path((String) split.getPath());
return new FileSplit(path,split.getOffset(),split.getLength(), new String[]{"localhost"});
}
public static WorkerId createWorkerId(String name) {
byte[] buff = new byte[32];
System.arraycopy(name.getBytes(), 0, buff, 0, name.length());
return new WorkerId(buff);
}
public static void mergeConfigs(StartupConfiguration sourceConf,
Configuration destConf) {
if (sourceConf == null || destConf == null)
return;
Map confMap = sourceConf.getOther();
if (confMap == null)
return;
for (Map.Entry entry : confMap.entrySet()) {
destConf.set(entry.getKey().toString(), entry.getValue().toString());
}
}
public static void mergeConfigs(Configuration sourceConf, Configuration destConf) {
if (sourceConf == null || destConf == null)
return;
for (Map.Entry entry : sourceConf) {
if (destConf.get(entry.getKey()) == null) {
destConf.set(entry.getKey(), entry.getValue());
}
}
}
public static void mergeConfigs(Map sourceConf, Configuration destConf) {
if (sourceConf == null || destConf == null)
return;
for(Map.Entry entry : sourceConf.entrySet()) {
if (destConf.get(entry.getKey().toString()) == null) {
destConf.set(entry.getKey().toString(), entry.getValue().toString());
}
}
}
public static void mergeConfigs(Map sourceConf, StartupConfiguration destConf) {
if (sourceConf == null || destConf == null)
return;
Map confMap = destConf.getOther();
if (confMap == null)
confMap = new HashMap();
for (Map.Entry entry : sourceConf.entrySet()) {
if (! confMap.containsKey(entry.getKey()))
confMap.put(entry.getKey(), entry.getValue());
}
destConf.setOther(confMap);
}
private static void copyToFs(Configuration conf, String local, String remote) throws IOException {
FileSystem fs = FileSystem.get(conf);
Path src = new Path(local);
Path dst = fs.makeQualified(new Path(remote));
LOG.debug("Copying to filesystem, src=" + src.toString() + ", dst="
+ dst);
fs.copyFromLocalFile(false, true, src, dst);
}
public static String getFileName(String f) {
return new Path(f).getName();
}
public static String getAppTempDirectory(ApplicationId appId, String appName) {
return "/tmp/" + appName + "/" + appId;
}
/*
* Copy local file to a named-file in remote FS
*/
public static void copyLocalResourceToFs(String src, String dst, Configuration conf,
ApplicationId appId, String appName) throws IOException {
String tempDir = getAppTempDirectory(appId, appName);
LOG.debug("Using temporary directory: " + tempDir);
copyToFs(conf, src, tempDir + "/" + dst);
}
/*
* Copy batch of resources based on Properties. Destination name will be same.
*/
public static void copyLocalResourcesToFs(Properties props, Configuration conf,
ApplicationId appId, String appName) throws IOException {
String tempDir = getAppTempDirectory(appId, appName);
String file;
LOG.debug("Using temporary directory: " + tempDir);
// Our JAR
file = props.getProperty(ConfigFields.JAR_PATH);
copyToFs(conf, file, tempDir + "/" + getFileName(file));
// User JAR
file = props.getProperty(ConfigFields.APP_JAR_PATH);
copyToFs(conf, file, tempDir + "/" + getFileName(file));
// Libs
for (String f : props.getProperty(ConfigFields.APP_LIB_PATH, "").split(",")) {
if (f.trim().equals(""))
continue;
copyToFs(conf, f, tempDir + "/" + getFileName(f));
}
}
public static String getPathForResource(String loc,
ApplicationId appId, String appName) {
return getAppTempDirectory(appId, appName) + "/" + getFileName(loc);
}
public static Map getLocalResourcesForApplication(
Configuration conf, ApplicationId appId, String appName,
Properties props, LocalResourceVisibility visibility) throws IOException {
List resources = new ArrayList();
Map localResources = new HashMap();
resources.add(getPathForResource(props.getProperty(ConfigFields.JAR_PATH), appId, appName)); // Our app JAR
resources.add(getPathForResource(props.getProperty(ConfigFields.APP_JAR_PATH), appId, appName)); // User app JAR
resources.add(getPathForResource(ConfigFields.APP_CONFIG_FILE, appId, appName)); // Our application configuration
resources.add(getPathForResource("log4j.properties", appId, appName));
// Libs
String libs = props.getProperty(ConfigFields.APP_LIB_PATH);
if (libs != null && !libs.isEmpty()) {
for (String s : libs.split(",")) {
resources.add(getPathForResource(s, appId, appName));
}
}
FileSystem fs = FileSystem.get(conf);
Path fsPath;
FileStatus fstat;
// Convert to local resource list
for (String resource : resources) {
try {
fsPath = new Path(resource);
fstat = fs.getFileStatus(fsPath);
LOG.debug("Processing local resource=" + fstat.getPath());
//System.out.println("IR: Utils > Converting to local resource: " + fstat.getPath() );
LocalResource localResource = Records.newRecord(LocalResource.class);
localResource.setResource(ConverterUtils.getYarnUrlFromPath(fstat.getPath()));
localResource.setSize(fstat.getLen());
localResource.setTimestamp(fstat.getModificationTime());
localResource.setVisibility(visibility);
localResource.setType(LocalResourceType.FILE);
localResources.put(fsPath.getName(), localResource);
} catch (FileNotFoundException ex) {
LOG.warn("Unable to copy file " + resource + ": File not found.");
}
}
return localResources;
}
public static Map getEnvironment(Configuration conf, Properties props) {
Map env = new LinkedHashMap();
// First lets get any arbitrary environment variables in the config
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy