|
| 1 | +/* |
| 2 | +* Copyright (C) Mellanox Technologies Ltd. 2020. ALL RIGHTS RESERVED. |
| 3 | +* See file LICENSE for terms. |
| 4 | +*/ |
| 5 | +package org.apache.spark.shuffle.ucx.perf |
| 6 | + |
| 7 | +import java.net.{InetAddress, InetSocketAddress, ServerSocket, Socket} |
| 8 | +import java.nio.ByteBuffer |
| 9 | +import java.util.concurrent.TimeUnit |
| 10 | +import java.util.concurrent.atomic.AtomicInteger |
| 11 | + |
| 12 | +import org.apache.commons.cli.{GnuParser, HelpFormatter, Options} |
| 13 | +import org.apache.spark.SparkConf |
| 14 | +import org.apache.spark.shuffle.ucx._ |
| 15 | +import org.apache.spark.shuffle.ucx.memory.MemoryPool |
| 16 | +import org.apache.spark.util.Utils |
| 17 | + |
| 18 | +object UcxShuffleTransportPerfTool { |
| 19 | + private val HELP_OPTION = "h" |
| 20 | + private val ADDRESS_OPTION = "a" |
| 21 | + private val NUM_BLOCKS_OPTION = "n" |
| 22 | + private val SIZE_OPTION = "s" |
| 23 | + private val PORT_OPTION = "p" |
| 24 | + private val ITER_OPTION = "i" |
| 25 | + private val MEMORY_TYPE_OPTION = "m" |
| 26 | + private val NUM_THREADS_OPTION = "t" |
| 27 | + |
| 28 | + private val ucxShuffleConf = new UcxShuffleConf(new SparkConf()) |
| 29 | + private val transport = new UcxShuffleTransport(ucxShuffleConf, "e") |
| 30 | + private val workerAddress = transport.init() |
| 31 | + private var memoryPool: MemoryPool = transport.memoryPool |
| 32 | + |
| 33 | + case class TestBlockId(id: Int) extends BlockId |
| 34 | + |
| 35 | + case class PerfOptions(remoteAddress: InetSocketAddress, numBlocks: Int, blockSize: Long, |
| 36 | + serverPort: Int, numIterations: Int, numThreads: Int) |
| 37 | + |
| 38 | + private def initOptions(): Options = { |
| 39 | + val options = new Options() |
| 40 | + options.addOption(HELP_OPTION, "help", false, |
| 41 | + "display help message") |
| 42 | + options.addOption(ADDRESS_OPTION, "address", true, |
| 43 | + "address of the remote host") |
| 44 | + options.addOption(NUM_BLOCKS_OPTION, "num-blocks", true, |
| 45 | + "number of blocks to transfer. Default: 1") |
| 46 | + options.addOption(SIZE_OPTION, "block-size", true, |
| 47 | + "size of block to transfer. Default: 4m") |
| 48 | + options.addOption(PORT_OPTION, "server-port", true, |
| 49 | + "server port. Default: 12345") |
| 50 | + options.addOption(ITER_OPTION, "num-iterations", true, |
| 51 | + "number of iterations. Default: 5") |
| 52 | + options.addOption(NUM_THREADS_OPTION, "num-threads", true, |
| 53 | + "number of threads. Default: 1") |
| 54 | + options.addOption(MEMORY_TYPE_OPTION, "memory-type", true, |
| 55 | + "memory type: host (default), cuda") |
| 56 | + } |
| 57 | + |
| 58 | + private def parseOptions(args: Array[String]): PerfOptions = { |
| 59 | + val parser = new GnuParser() |
| 60 | + val options = initOptions() |
| 61 | + val cmd = parser.parse(options, args) |
| 62 | + |
| 63 | + if (cmd.hasOption(HELP_OPTION)) { |
| 64 | + new HelpFormatter().printHelp("UcxShufflePerfTool", options) |
| 65 | + System.exit(0) |
| 66 | + } |
| 67 | + |
| 68 | + val inetAddress = if (cmd.hasOption(ADDRESS_OPTION)) { |
| 69 | + val Array(host, port) = cmd.getOptionValue(ADDRESS_OPTION).split(":") |
| 70 | + new InetSocketAddress(host, Integer.parseInt(port)) |
| 71 | + } else { |
| 72 | + null |
| 73 | + } |
| 74 | + |
| 75 | + val serverPort = Integer.parseInt(cmd.getOptionValue(PORT_OPTION, "12345")) |
| 76 | + |
| 77 | + val numIterations = Integer.parseInt(cmd.getOptionValue(ITER_OPTION, "5")) |
| 78 | + |
| 79 | + val threadsNumber = Integer.parseInt(cmd.getOptionValue(NUM_THREADS_OPTION, "1")) |
| 80 | + |
| 81 | + if (cmd.hasOption(MEMORY_TYPE_OPTION) && cmd.getOptionValue(MEMORY_TYPE_OPTION) == "cuda") { |
| 82 | + val className = "org.apache.spark.shuffle.ucx.GpuMemoryPool" |
| 83 | + val cls = Utils.classForName(className) |
| 84 | + memoryPool = cls.getConstructor().newInstance().asInstanceOf[MemoryPool] |
| 85 | + } |
| 86 | + |
| 87 | + PerfOptions(inetAddress, |
| 88 | + Integer.parseInt(cmd.getOptionValue(NUM_BLOCKS_OPTION, "1")), |
| 89 | + Utils.byteStringAsBytes(cmd.getOptionValue(SIZE_OPTION, "4m")), |
| 90 | + serverPort, numIterations, threadsNumber) |
| 91 | + } |
| 92 | + |
| 93 | + private def startServer(perfOptions: PerfOptions): Unit = { |
| 94 | + val blocks: Seq[Block] = (0 until perfOptions.numBlocks).map { _ => |
| 95 | + val block = memoryPool.get(perfOptions.blockSize) |
| 96 | + new Block { |
| 97 | + override def getMemoryBlock: MemoryBlock = |
| 98 | + block |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + val blockIds = (0 until perfOptions.numBlocks).map(i => TestBlockId(i)) |
| 103 | + blockIds.zip(blocks).foreach { |
| 104 | + case (blockId, block) => transport.register(blockId, block) |
| 105 | + } |
| 106 | + |
| 107 | + val serverSocket = new ServerSocket(perfOptions.serverPort) |
| 108 | + |
| 109 | + println(s"Waiting for connections on " + |
| 110 | + s"${InetAddress.getLocalHost.getHostName}:${perfOptions.serverPort} ") |
| 111 | + |
| 112 | + val clientSocket = serverSocket.accept() |
| 113 | + val out = clientSocket.getOutputStream |
| 114 | + val in = clientSocket.getInputStream |
| 115 | + |
| 116 | + val buf = ByteBuffer.allocate(workerAddress.capacity()) |
| 117 | + buf.put(workerAddress) |
| 118 | + buf.flip() |
| 119 | + |
| 120 | + out.write(buf.array()) |
| 121 | + out.flush() |
| 122 | + |
| 123 | + println(s"Sending worker address to ${clientSocket.getInetAddress}") |
| 124 | + |
| 125 | + buf.flip() |
| 126 | + |
| 127 | + in.read(buf.array()) |
| 128 | + clientSocket.close() |
| 129 | + serverSocket.close() |
| 130 | + |
| 131 | + blocks.foreach(block => memoryPool.put(block.getMemoryBlock)) |
| 132 | + blockIds.foreach(transport.unregister) |
| 133 | + transport.close() |
| 134 | + } |
| 135 | + |
| 136 | + private def startClient(perfOptions: PerfOptions): Unit = { |
| 137 | + val socket = new Socket(perfOptions.remoteAddress.getHostName, |
| 138 | + perfOptions.remoteAddress.getPort) |
| 139 | + |
| 140 | + val buf = new Array[Byte](4096) |
| 141 | + val readSize = socket.getInputStream.read(buf) |
| 142 | + val executorId = "1" |
| 143 | + val workerAddress = ByteBuffer.allocateDirect(readSize) |
| 144 | + |
| 145 | + workerAddress.put(buf, 0, readSize) |
| 146 | + println("Received worker address") |
| 147 | + |
| 148 | + transport.addExecutor(executorId, workerAddress) |
| 149 | + |
| 150 | + val resultSize = perfOptions.numBlocks * perfOptions.blockSize |
| 151 | + val resultMemory = memoryPool.get(resultSize) |
| 152 | + |
| 153 | + val blockIds = (0 until perfOptions.numBlocks).map(i => TestBlockId(i)) |
| 154 | + |
| 155 | + (1 to perfOptions.numIterations).foreach { (i: Int) => { |
| 156 | + (0 until perfOptions.numThreads).par.foreach {(t: Int) => { |
| 157 | + val completed = new AtomicInteger(0) |
| 158 | + var elapsedTime: Long = 0L |
| 159 | + |
| 160 | + val mem = new Array[MemoryBlock](perfOptions.numBlocks) |
| 161 | + val callbacks = new Array[OperationCallback](perfOptions.numBlocks) |
| 162 | + (0 until perfOptions.numBlocks).foreach(j => { |
| 163 | + mem(j) = MemoryBlock(resultMemory.address + j * perfOptions.blockSize, perfOptions.blockSize) |
| 164 | + callbacks(j) = (result: OperationResult) => { |
| 165 | + elapsedTime += result.getStats.get.getElapsedTimeNs |
| 166 | + completed.incrementAndGet() |
| 167 | + } |
| 168 | + }) |
| 169 | + |
| 170 | + transport.fetchBlocksByBlockIds(executorId, blockIds, mem, callbacks) |
| 171 | + |
| 172 | + while (completed.get() != perfOptions.numBlocks) { |
| 173 | + transport.progress() |
| 174 | + } |
| 175 | + val totalTime = if (elapsedTime < TimeUnit.MILLISECONDS.toNanos(1)) { |
| 176 | + s"$elapsedTime ns" |
| 177 | + } else { |
| 178 | + s"${TimeUnit.NANOSECONDS.toMillis(elapsedTime)} ms" |
| 179 | + } |
| 180 | + val throughput: Double = (resultSize / 1024.0D / 1024.0D / 1024.0D) / (elapsedTime / 1e9D) |
| 181 | + |
| 182 | + println(f"${s"[$i/${perfOptions.numIterations}]"}%12s" + |
| 183 | + s" numBlocks: ${perfOptions.numBlocks}" + |
| 184 | + s" size: ${Utils.bytesToString(perfOptions.blockSize)}," + |
| 185 | + s" total size: ${Utils.bytesToString(resultSize)}," + |
| 186 | + f" time: $totalTime%3s" + |
| 187 | + f" throughput: $throughput%.3f GB/s") |
| 188 | + }} |
| 189 | + }} |
| 190 | + |
| 191 | + val out = socket.getOutputStream |
| 192 | + out.write(buf) |
| 193 | + out.flush() |
| 194 | + out.close() |
| 195 | + socket.close() |
| 196 | + |
| 197 | + memoryPool.put(resultMemory) |
| 198 | + transport.close() |
| 199 | + } |
| 200 | + |
| 201 | + def main(args: Array[String]): Unit = { |
| 202 | + val perfOptions = parseOptions(args) |
| 203 | + |
| 204 | + if (perfOptions.remoteAddress == null) { |
| 205 | + startServer(perfOptions) |
| 206 | + } else { |
| 207 | + startClient(perfOptions) |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | +} |
0 commit comments