|
| 1 | +// Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import ArgumentParser |
| 16 | +import Foundation |
| 17 | +import TensorFlow |
| 18 | + |
| 19 | +struct ComplexConstant { |
| 20 | + let real: Float |
| 21 | + let imaginary: Float |
| 22 | +} |
| 23 | + |
| 24 | +func juliaSet( |
| 25 | + iterations: Int, constant: ComplexConstant, tolerance: Float, region: ComplexRegion, |
| 26 | + imageSize: ImageSize, device: Device |
| 27 | +) -> Tensor<Float> { |
| 28 | + let xs = Tensor<Float>( |
| 29 | + linearSpaceFrom: region.realMinimum, to: region.realMaximum, count: imageSize.width, on: device |
| 30 | + ).broadcasted(to: [imageSize.width, imageSize.height]) |
| 31 | + let ys = Tensor<Float>( |
| 32 | + linearSpaceFrom: region.imaginaryMaximum, to: region.imaginaryMinimum, count: imageSize.height, |
| 33 | + on: device |
| 34 | + ).expandingShape(at: 1).broadcasted(to: [imageSize.width, imageSize.height]) |
| 35 | + var Z = ComplexTensor(real: xs, imaginary: ys) |
| 36 | + let C = ComplexTensor( |
| 37 | + real: Tensor<Float>(repeating: constant.real, shape: xs.shape, on: device), |
| 38 | + imaginary: Tensor<Float>(repeating: constant.imaginary, shape: xs.shape, on: device)) |
| 39 | + var divergence = Tensor<Float>(repeating: Float(iterations), shape: xs.shape, on: device) |
| 40 | + |
| 41 | + // We'll make sure the initialization of these tensors doesn't carry |
| 42 | + // into the trace for the first iteration. |
| 43 | + LazyTensorBarrier() |
| 44 | + |
| 45 | + let start = Date() |
| 46 | + var firstIteration = Date() |
| 47 | + |
| 48 | + for iteration in 0..<iterations { |
| 49 | + Z = Z * Z + C |
| 50 | + |
| 51 | + let aboveThreshold = abs(Z) .> tolerance |
| 52 | + divergence = divergence.replacing( |
| 53 | + with: min(divergence, Float(iteration)), where: aboveThreshold) |
| 54 | + |
| 55 | + // We're cutting the trace to be a single iteration. |
| 56 | + LazyTensorBarrier() |
| 57 | + if iteration == 1 { |
| 58 | + firstIteration = Date() |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + print( |
| 63 | + "Total calculation time: \(String(format: "%.3f", Date().timeIntervalSince(start))) seconds") |
| 64 | + print( |
| 65 | + "Time after first iteration: \(String(format: "%.3f", Date().timeIntervalSince(firstIteration))) seconds" |
| 66 | + ) |
| 67 | + |
| 68 | + return divergence |
| 69 | +} |
| 70 | + |
| 71 | +extension ComplexConstant: ExpressibleByArgument { |
| 72 | + init?(argument: String) { |
| 73 | + let subArguments = argument.split(separator: ",").compactMap { Float(String($0)) } |
| 74 | + guard subArguments.count >= 2 else { return nil } |
| 75 | + |
| 76 | + self.real = subArguments[0] |
| 77 | + self.imaginary = subArguments[1] |
| 78 | + } |
| 79 | + |
| 80 | + var defaultValueDescription: String { |
| 81 | + "\(self.real),\(self.imaginary)" |
| 82 | + } |
| 83 | +} |
0 commit comments