dynclock.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 nelisky.btc@gmail.com
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "dynclock.h"
  11. #include "miner.h"
  12. void dclk_prepare(struct dclk_data *data)
  13. {
  14. *data = (struct dclk_data){.freqM=0};
  15. }
  16. void dclk_msg_freqchange(const char *repr, int oldFreq, int newFreq, const char *tail)
  17. {
  18. applog(LOG_NOTICE, "%s: Frequency %s from %u to %u Mhz%s",
  19. repr,
  20. (oldFreq > newFreq ? "dropped" : "raised "),
  21. oldFreq, newFreq,
  22. tail ?: ""
  23. );
  24. }
  25. bool dclk_updateFreq(struct dclk_data *data, dclk_change_clock_func_t changeclock, struct thr_info *thr)
  26. {
  27. struct cgpu_info *cgpu = thr->cgpu;
  28. uint8_t freqMDefault = data->freqMDefault;
  29. int i, maxM, bestM;
  30. double bestR, r;
  31. bool rv = true;
  32. if (freqMDefault > data->freqMaxM)
  33. // This occurs when the device in question adjusts its MaxM down due to temperature or similar reasons
  34. freqMDefault = data->freqMaxM;
  35. for (i = 0; i < data->freqMaxM; i++)
  36. if (data->maxErrorRate[i + 1] * i < data->maxErrorRate[i] * (i + 20))
  37. data->maxErrorRate[i + 1] = data->maxErrorRate[i] * (1.0 + 20.0 / i);
  38. maxM = 0;
  39. while (maxM < freqMDefault && data->maxErrorRate[maxM + 1] < DCLK_MAXMAXERRORRATE)
  40. maxM++;
  41. while (maxM < data->freqMaxM && data->errorWeight[maxM] > 150 && data->maxErrorRate[maxM + 1] < DCLK_MAXMAXERRORRATE)
  42. maxM++;
  43. bestM = 0;
  44. bestR = 0;
  45. for (i = 0; i <= maxM; i++) {
  46. r = (i + 1 + (i == data->freqM? DCLK_ERRORHYSTERESIS: 0)) * (1 - data->maxErrorRate[i]);
  47. if (r > bestR) {
  48. bestM = i;
  49. bestR = r;
  50. }
  51. }
  52. if (bestM != data->freqM) {
  53. rv = changeclock(thr, bestM);
  54. }
  55. maxM = freqMDefault;
  56. while (maxM < data->freqMaxM && data->errorWeight[maxM + 1] > 100)
  57. maxM++;
  58. if ((bestM < (1.0 - DCLK_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) {
  59. applog(LOG_ERR, "%s %u: frequency drop of %.1f%% detect. This may be caused by overheating. FPGA is shut down to prevent damage.",
  60. cgpu->api->name, cgpu->device_id,
  61. (1.0 - 1.0 * bestM / maxM) * 100);
  62. return false;
  63. }
  64. return rv;
  65. }
  66. void dclk_gotNonces(struct dclk_data *data)
  67. {
  68. data->errorCount[data->freqM] *= 0.995;
  69. data->errorWeight[data->freqM] = data->errorWeight[data->freqM] * 0.995 + 1.0;
  70. }
  71. void dclk_errorCount(struct dclk_data *data, double portion)
  72. {
  73. data->errorCount[data->freqM] += portion;
  74. }
  75. void dclk_preUpdate(struct dclk_data *data)
  76. {
  77. data->errorRate[data->freqM] = data->errorCount[data->freqM] / data->errorWeight[data->freqM] * (data->errorWeight[data->freqM] < 100 ? data->errorWeight[data->freqM] * 0.01 : 1.0);
  78. if (data->errorRate[data->freqM] > data->maxErrorRate[data->freqM])
  79. data->maxErrorRate[data->freqM] = data->errorRate[data->freqM];
  80. }