0

I want to reduce the overhead of the MonteCarlo Simulation in Swift language by performing concurrent programming on the outer loop while the inner loop would be executed normally. I got some info from the post

Using Grand Central Dispatch in Swift to parallelize and speed up “for" loops?, but seems to be outdated and not applying to swift 3. My code is as follows:

//constants
    let outerCount = 10
    let innerCount = 300
    //variables
    var firstMove = -1
    var num = -1
    var sum = [Int](repeating: 0, count: 9)
    var wincount = [[Int]](repeating:[Int](repeating:0, count:9),count:10)
    var candidateCount = 0
    var candidate = [Int](repeating: 0, count: 9)
    var maxWins = -2147483648
    var winners = [Int](repeating: 0, count: outerCount)

    //codeRiddle to solve

    winners.withUnsafeMutableBufferPointer { winnersPtr -> Void in

        DispatchQueue.concurrentPerform(iterations: outerCount) {
            outerIdx -> Void in

            for _ in 0...innerCount
            {
                firstMove = -1
                for _ in 0...8
                {
                    if firstMove == -1
                    {
                        firstMove = Int(arc4random()%UInt32(9))
                    }
                }
                num = Int(arc4random()%UInt32(3))
                if num == 0
                {
                    wincount[outerIdx][firstMove] += 0
                }
                else if num == 1
                {
                    wincount[outerIdx][firstMove] += -10
                }
                else
                {
                    wincount[outerIdx][firstMove] += 1
                }
            }
        }
    }

    for i in 0...8
    {
        for j in 0...9
        {
            sum[i] += wincount[j][i]
        }
    }

    for  i in 0...8
    {
        if sum[i] > maxWins
        {
            maxWins = sum[i]
        }
    }

    for i in 0...8
    {
        if sum[i] == maxWins
        {
            candidate[candidateCount] = i
            candidateCount += 1
        }
    }
    print(candidate[Int(arc4random()%UInt32(candidateCount))])
Community
  • 1
  • 1
  • The question you're referring to has an appropriate answer. You just need to use Swift 3 syntax. – werediver Jan 17 '17 at 07:37
  • See http://stackoverflow.com/questions/39948082/long-cycle-blocks-application and http://stackoverflow.com/questions/39590128/what-is-equivalent-of-dispatch-apply-in-swift-3 for the Swift 3 equivalent of dispatch_apply(). – Martin R Jan 17 '17 at 07:41
  • @MartinR I applied the extract of these questions you suggested to the code but it even degraded my previous code. If you can please check out my code and show me the exact solution, I've been stuck since two days. – Castor Troy Jan 17 '17 at 07:46
  • Then *show* what you tried. Your posted code is not self-contained and does not contain any GCD calls. A [mcve] is needed. – Martin R Jan 17 '17 at 07:50
  • @MartinR Please see the edit – Castor Troy Jan 17 '17 at 07:59
  • @MartinR Two more edits made now. Sorry for inconvenience. – Castor Troy Jan 17 '17 at 08:03
  • Can someone plz provide a definite solution. Much needed. – Castor Troy Jan 18 '17 at 02:56
  • @CastorTroy: *Self-contained* means that someone else can copy/paste into Xcode an run it. That is not the case here (what is `Board` or `Box_Value`?)– *Minimal* means that you make the code as short as possible while still demonstrating the problem. I cannot judge if your example is minimal because you don't *explain* what your code does. – Martin R Jan 18 '17 at 06:36
  • @MartinR When Can I expect an answer from you sir? – Castor Troy Jan 19 '17 at 02:21
  • @MartinR My development progress is stuck at this point for almost a week now. If there is any mistake still from my side in posting the question please let me know. I expect the answer soon sir. – Castor Troy Jan 19 '17 at 11:49
  • @CastorTroy: Posting answers on SO is *voluntary*. If someone finds your question interesting and has the time to investigate it then (s)he might post an answer. You cannot *expect* an answer only if it is important for *your* work. It is your development – perhaps you will make money out of it, but we will certainly not. If you need a solution in a fixed timeframe then hire someone and pay for it. – Martin R Jan 19 '17 at 12:16
  • @MartinR And may I ask where can I hire someone for just one problem and not for a project as whole, plus if you were not finding the question interesting sir you should not have interfered at first place and since on your requests I sincerely re-edited the question multiple times so I think it becomes your duty to anser back I suppose. May be I make money out of this you don't, but what you earn is my respect sire. – Castor Troy Jan 19 '17 at 12:47

0 Answers0